Actionbarsherlock back button and smartphone back

2019-04-17 10:38发布

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();

2条回答
放我归山
2楼-- · 2019-04-17 11:03

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().

查看更多
时光不老,我们不散
3楼-- · 2019-04-17 11:09

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.

查看更多
登录 后发表回答