简体   繁体   English

Android后退按钮无法正常工作

[英]Android back button not working properly

When i push the back button on the phone it opens the pause activity as intended but it also goes to the previous activity(i can see this because pause activity style is Theme.AppCompat.Dialog. What i want is just open the pause activity but in the backround to be the current activity not the previous one.The code: 当我按电话上的后退按钮时,它会按预期打开暂停活动,但也会转到上一个活动(我可以看到此信息,因为暂停活动的样式是Theme.AppCompat.Dialog。我想要的只是打开暂停活动,但在背景中是当前活动,而不是上一个活动。代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer_2);
    //...
}

//...

@Override
public void onBackPressed()
{
    super.onBackPressed();
    startActivity(new Intent(timer_2.this, timer_2_pause.class));
    finish();

}

In case you wish to kill previous activity use this: 如果您想杀死以前的活动,请使用以下命令:

@Override
public void onBackPressed()
{
    startActivity(new Intent(timer_2.this, timer_2_pause.class));
    finish();
}

If you want to keep that activity in back stack use this: 如果要将该活动保留在堆栈中,请使用以下命令:

@Override
public void onBackPressed()
{
    startActivity(new Intent(timer_2.this, timer_2_pause.class));
}

Try this: 尝试这个:

@Override
public void onBackPressed() {
    Intent i=new Intent(timer_2.this,timer_2_pause.class);
    startActivity(i);
    finish();
    super.onBackPressed();
}

Its because you are calling finish() to close the current activity. 这是因为您正在调用finish()以关闭当前活动。 Remove finish() and it wont close the current activity. 删除finish()并不会关闭当前活动。

You should use onBackPress() like this : 您应该像这样使用onBackPress()

@Override
public void onBackPressed()
{
   // super.onBackPressed();
   // finish();
    startActivity(new Intent(timer_2.this, timer_2_pause.class));


}

Use this just... 只是用这个...

@Override
public void onBackPressed()
{
     //super.onBackPressed(); dont use this..
     finish();
     startActivity(new Intent(timer_2.this, timer_2_pause.class));


  }

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

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