简体   繁体   English

Actionbarsherlock后退按钮和智能手机后退按钮

[英]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(); 但是它总是崩溃,当我输入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: 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(); 已经尝试了许多方法,但仅使用finish()无效。

In your Second Activity just remove your intent and startActivity stuff. 在您的第二个活动中,只需删除您的intent和startActivity内容即可。 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. finish()将从后堆栈中删除该活动,因此在开始要让用户通过按“后退”按钮返回的新活动时,不要使用该活动。

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. 您不想从SecondActivity重新启动Principal活动,而只希望第二个活动完成并返回上一个活动。 Try replacing the following code in SecondActivity -- 尝试在SecondActivity替换以下代码-

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

with just finish() . 仅带有finish()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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