Manos Rasidakis
Manos Rasidakis

Reputation: 3

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

}

Upvotes: 0

Views: 6857

Answers (5)

Maksym V.
Maksym V.

Reputation: 2937

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

Upvotes: 3

Komal12
Komal12

Reputation: 3348

Try this:

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

Upvotes: 1

Santanu Sur
Santanu Sur

Reputation: 11487

Use this just...

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


  }

Upvotes: 0

Abhishek kumar
Abhishek kumar

Reputation: 4445

You should use onBackPress() like this :

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


}

Upvotes: 0

tonedblue
tonedblue

Reputation: 164

Its because you are calling finish() to close the current activity. Remove finish() and it wont close the current activity.

Upvotes: 0

Related Questions