-1

i have three activities A, B and C. when i press a button in activity A it starts activity B and when i press a button in activity B it starts activity C. up to here there is no problem.

but when i press the back button on activity C it should returns me back to B. but what happens could be described as follows:

1st time B starts C, then C goes back to B
2nd time B starts C, then C goes back to B
3rd time B starts C, then C goes back to B
4th time B starts C, then C goes back to B

and so on,

now, when I press the back button on B it should navigates me back to A, but what happens is, when i press the back button on B it returns me to B over and over again to B as much as B started C. if the activity B started C 5 times, then when press the back button on B it returns me 5 times to the same activity B and in the 6th time it returns me to A

please let me know why that is happeneing and how to solve it

code

//in activity C
@Override
public void onBackPressed() {
    super.onBackPressed();

    new Intent(SchadenListeActivity.this, 
VersMoreDetailsModActivity.class);
    finish();
}

//in activity B
@Override
public void onBackPressed() {
    super.onBackPressed();

    new Intent(VersMoreDetailsModActivity.this,   
 VersicherungsListeActivity.class);
    finish();
}
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • 2
    post your code here – Quick learner Dec 28 '16 at 07:07
  • Possible duplicate of [How to manage multiple activity interactions in Android?](http://stackoverflow.com/questions/31578682/how-to-manage-multiple-activity-interactions-in-android) – Patel Pinkal Dec 28 '16 at 07:10
  • @quicklearner code posted please have a look – Amrmsmb Dec 28 '16 at 07:11
  • what about your button click code ? post it – Quick learner Dec 28 '16 at 07:12
  • @user2121 Starting new intents on backpress is not a good idea.. Instead use `startActivityForResult()` which will give callbacks on `onActivityResult()` – Sunil Sunny Dec 28 '16 at 07:15
  • @sunilsunny no need to make it complex. Andrroid handles the backpress event. so while going to next activity, no need to finish it. – Sagar Patil Dec 28 '16 at 07:19
  • @SagarPatil You are right about it .Only suggested that if he need any data in return from class C to B. – Sunil Sunny Dec 28 '16 at 07:23
  • You have several answers suggesting that you remove the overridden `onBackPressed()` code. This is the correct solution. If this is not working for you, please describe exactly what you see after removing that code from ALL activities. – David Wasser Dec 28 '16 at 11:06

4 Answers4

0

Include parent activities in your manifest with the attribute android:parentActivityName="com.example.myfirstapp

Here's some more info on up-navigation http://developer.android.com/design/patterns/navigation.html

Tom James
  • 279
  • 3
  • 16
0

When you go from A->B->C dont finish any of the activity. Delete the onBackPressed() code, no need to handle it.

Sagar Patil
  • 1,400
  • 15
  • 29
0

Each time in onBackPressed()method you are starting a new Activity and android works on Backstack Model..

Let me explain this

Activity-A  Activity-B  Activity-C

So each time you are clicking OnBackPressed the stack is showing this behaviour.

Stack [ Activity-A, Activity-B, Activity-C,(Backpressed)(POP Activity-C), Activity-B, Activity-C, (Backpressed)(POP - Activity-C), Activity - B, Activity - C, (BackPressed)(POP - Activity-C), Activity - B, Activity -C...]

So Remaining Stack will be like

Stack [ Activity-A, Activity-B, Activity-B, Activity - B, Activity - B, Activity -C...]

Just call finish(); not new Intent.

androgo
  • 564
  • 2
  • 8
0

You are creating new instances of Activity in onBackPressed.Simply,remove the intent statements from your activities.

//in activity C

@Override
public void onBackPressed() 
 {
 super.onBackPressed();
 finish();
}

//in activity B

@Override
public void onBackPressed() 
 {
 super.onBackPressed();
 finish();
}
Android Geek
  • 8,956
  • 2
  • 21
  • 35