简体   繁体   中英

Actionbarsherlock back button and smartphone back button

The problem:

I have the main activity that I want to callback when I press back button from either smartphone and actionbar on the second activity. But it always crash, it just work when I put a finish(); in the main activity, but If I do that then the back button from smartphone doesn't work properly.

MainActivity:

public class Principal extends SherlockActivity {

    public static int THEME = R.style.Theme_Sherlock;
    private Button entrar;
    private Button cadastrar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            //setTheme(Principal.THEME); //Used for theme switching in samples
            super.onCreate(savedInstanceState);
            setContentView(R.layout.home);

            entrar = (Button)findViewById(R.id.entrar); 
            entrar.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {


                    startActivity(new Intent(Principal.this,LoginActivity.class)); 
                    finish();
                }
            });
            cadastrar = (Button)findViewById(R.id.cadastrar_home); 
            cadastrar.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {


                    Intent intent = new Intent(Principal.this, RegisterActivity.class);
                    startActivity(intent);
                    //finish();
                }
            });
        }

SecondActicity:

public class RegisterActivity extends SherlockActivity{

    protected void onCreate(Bundle savedInstanceState) {
        setTheme(Principal.THEME); //Used for theme switching in samples
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) 
        {

        case android.R.id.home:
             // Do whatever you want, e.g. finish()
            Intent intent = new Intent(this, Principal.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
             break;

          }
        return true;
    }
}

Already tried many ways and none works, only with finish();

In your Second Activity just remove your intent and startActivity stuff. Only need:

case android.R.id.home:
    finish();
break;

finish() will remove that activity from the back-stack so don't use it when starting a new activity that you want the user to get back to by pressing the back button.

You do not want to relaunch you Principal activity from SecondActivity , you just want the second activity to finish and return to the previous activity. Try replacing the following code in SecondActivity --

Intent intent = new Intent(this, Principal.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

with just finish() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM