【问题标题】:Actionbarsherlock back button and smartphone back buttonActionbarsherlock 后退按钮和智能手机后退按钮
【发布时间】:2012-12-19 15:20:02
【问题描述】:

问题:

当我从智能手机和第二个活动的操作栏按下返回按钮时,我有想要回调的主要活动。但它总是崩溃,当我放一个finish()时它才起作用;在主要活动中,但如果我这样做,那么智能手机的后退按钮将无法正常工作。

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;
    }
}

已经尝试了很多方法都没有,只有finish();

【问题讨论】:

    标签: android android-activity actionbarsherlock android-lifecycle


    【解决方案1】:

    在您的第二个活动中,只需删除您的意图和 startActivity 内容。只需要:

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

    finish() 将从返回堆栈中删除该活动,因此在启动您希望用户通过按返回按钮返回的新活动时不要使用它。

    【讨论】:

    • 谢谢你的回答。
    【解决方案2】:

    您不想从SecondActivity 重新启动您的主要活动,您只想完成第二个活动并返回到上一个活动。尝试替换SecondActivity中的以下代码--

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

    只有finish()

    【讨论】:

      猜你喜欢
      • 2018-08-02
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 2015-10-14
      • 1970-01-01
      相关资源
      最近更新 更多