-1

I have a next problem. Let's assume i have three Activities : A, B and C. I can come from A to C, and from B to C. Then when i pressed button in Activity C i want to go back to the previous Activity, or A or B. I don't know which one was previous.

in A Activity :

Intent intent=new Intent(this, C.class);
Intent.PutExtra("FROM_A", "A")
StartActivity;

in B Activity :

Intent intent=new Intent(this, C.class);
Intent.PutExtra("FROM_B", "B")
StartActivity;

in Activity C:

public void onClick(View v) {
    onBackPressed();    // automatic know which Activity i came from(A or B)
} 

Please Help Me. What's Wrong?

Thank you for your answers.

Alexey
  • 41
  • 1
  • 6
  • 1
    Android maintain the `Activity` stack so when you call `onBackPressed()` method than it will automatically switch the activity which you came.For more detail please read this https://developer.android.com/guide/components/activities/tasks-and-back-stack.html – Farmer Mar 14 '17 at 05:40
  • HEY WHY YOU USING INTENT FOR THIS KIND OF WAY – Akash pasupathi Mar 14 '17 at 05:42
  • If you dont override back key it will automatically take you to the previous activity, if you go **A to C**, back key will bring **A** after **C** is closed, similarly, if you go **B to C**, back-key pressed in activity **C** will bring back activity **B**. – Talha Mar 14 '17 at 05:42
  • _Please mention reason for downvote and how this question can be improved._ – Talha Mar 14 '17 at 05:43
  • Possible duplicate of [Android: Go back to previous activity](http://stackoverflow.com/questions/4038479/android-go-back-to-previous-activity) – Mohammed Atif Mar 14 '17 at 05:46
  • Thank you for your answer. I have additional question. if i want to specify in code to which one of the Activities(not previous) go back. What is should do in this case? – Alexey Mar 14 '17 at 09:58

4 Answers4

2

in A Activity :

Intent intent=new Intent(this, C.class);
startActivity(intent);

in B Activity :

Intent intent=new Intent(this, C.class);
startActivity(intent);

in Activity C:

public void onClick(View v) {
    finish();    
} 
Mehul Gajjar
  • 431
  • 5
  • 20
  • Thank you for your answer. I have additional question. if i want to specify in code to which one of the Activities(not previous) go back. What is should do in this case? – Alexey Mar 14 '17 at 09:57
2

in Activity C:

public void onClick(View v) {
    finish();
} 
Shanto George
  • 994
  • 13
  • 26
  • Thank you for your answer. I have additional question. if i want to specify in code to which one of the Activities(not previous) go back. What is should do in this case? – Alexey Mar 14 '17 at 09:57
0

in Activity A :

    Intent intent=new Intent(this, C.class);
    intent.PutExtra("FROM_A", "A")
    startActivity(intent);
    finish();  

in Activity B :

    Intent intent=new Intent(this, C.class);
    intent.PutExtra("FROM_B", "B")
    startActivity(intent);
    finish();  

in Activity C:

    public void onClick(View v) {
      Intent intent=new Intent(this, YourActivity.class);
      startActivity(intent);
      finish();  
    } 
Darshan Soni
  • 1,779
  • 1
  • 12
  • 24
Komal12
  • 3,340
  • 4
  • 16
  • 25
  • 1
    I prefer using just `finish()` instead of making explicit call to lifecycle method – Mohammed Atif Mar 14 '17 at 05:45
  • Thank you for your answer. I have additional question. if i want to specify in code to which one of the Activities(not previous) go back. What is should do in this case? – Alexey Mar 14 '17 at 09:58
  • @Alexey Shpaner check updated code..call finish when you call new activity. And C activity specify your activity and call finish – Komal12 Mar 14 '17 at 10:05
0

Android Activity is based on the stack Algorithm and when you open new activity in you app the stack is push your activity and when you remove the activity the stack pop your activity. So when you go from A to C and B to C this activity added on to the stack and follow the last in first out .So simply you need to call the finish() method when button is back pressed. Thank you.

Rishabh Rawat
  • 1,083
  • 9
  • 15
  • Thank you for your answer. I have additional question. if i want to specify in code to which one of the Activities(not previous) go back. What is should do in this case? – Alexey Mar 14 '17 at 09:58
  • Alexey if you want to specify the specific activity.When you pressed the backed button you simply call the intent class with startActivity() method but always remember when you call the specific activity just call the finish() method in the present activity.if you not finish your present activity your app takes more RAM space. – Rishabh Rawat Mar 14 '17 at 13:34