简体   繁体   中英

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:

@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. Remove finish() and it wont close the current activity.

You should use onBackPress() like this :

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


  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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