簡體   English   中英

Actionbarsherlock后退按鈕和智能手機后退按鈕

[英]Actionbarsherlock back button and smartphone back button

問題:

當我在第二個活動中從智能手機和操作欄上按“后退”按鈕時,我有要回調的主要活動。 但是它總是崩潰,當我輸入finish()時它才起作用。 在主要活動中,但是如果執行此操作,則智能手機的后退按鈕將無法正常工作。

主要活動:

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()無效。

在您的第二個活動中,只需刪除您的intent和startActivity內容即可。 只需要:

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

finish()將從后堆棧中刪除該活動,因此在開始要讓用戶通過按“后退”按鈕返回的新活動時,不要使用該活動。

您不想從SecondActivity重新啟動Principal活動,而只希望第二個活動完成並返回上一個活動。 嘗試在SecondActivity替換以下代碼-

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

僅帶有finish()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM