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

由网友(时光深港少年梦i)分享简介:的问题: 我有我要回拨的时候我preSS后退按钮无论从智能手机和动作条上的第二个活动的主要活动。但它总是崩溃,当我把一个结束,它只是工作();在主要活动,但如果我这样做,然后从智能手机上的后退按钮无法正常工作。I have the main activity that I want to callback whe...

的问题:

我有我要回拨的时候我preSS后退按钮无论从智能手机和动作条上的第二个活动的主要活动。但它总是崩溃,当我把一个结束,它只是工作();在主要活动,但如果我这样做,然后从智能手机上的后退按钮无法正常工作。

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

推荐答案

在你的第二个活动只是删除你的意图和startActivity的东西。只需要:

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

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

完成()将删除从后面堆栈的活动,以便开始您希望用户能够回到由pressing后退按钮一个新的活动时,不使用它。

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.

阅读全文

相关推荐

最新文章