-2

I want to set the onBackPress button to start activities that started before orderly. If I don't finish activities that started before when I start another activity, I can show the activity that started before with finish() current activity. This is possible. Because I don't finish any activity. So finish() current activity will cause show activity that started before. But, RAM will be overload when I open 50 activities may be. So because of this problem I need to finish() activity that started before. On the other hand, I want to start the activity that started before orderly when clicking to the back button. For example

   ActivityA --> ActivityB --> ActivityC --> ActivityD

In this scenario, I finish ActivityA, ActivityB, ActivityC orderly. But I want to start ActivityC back press of the ActivityD and I want to start ActivityB on the back press of ActivityC.

Lastly, I have a complex path in the application. And, I navigate to the ActivityD and I finished the ActivityF when my current activity is ActivityF

 ActivityF --> ActivityD
 ActivityF finished

Current activity:

 ActivityD

In this scenario, I want to start the ActivityF not the ActivityC on back button of the ActivityD.

Normally:

 ActivityA --> ActivityB --> ActivityC --> ActivityD /// click back button
 ActivityA --> ActivityB --> ActivityC /// click back button
 ActivityA --> ActivityB /// click back button
 ActivityA

or

 ActivityA --> ActivityF --> ActivityD /// click back button
 ActivityA --> ActivityF /// click back button
 ActivityA

But I want to generate this scenario:

 ActivityA
 --> ActivityB
 --> ActivityC
 --> ActivityD /// click back button
 ActivityC <-- /// click back button
 ActivityB <-- /// click back button
 ActivityA 

or

 ActivityA
 --> ActivityF
 --> ActivityD /// click back button
 ActivityF <-- /// click back button
 ActivityA <--

Update:

I found the solution here

K.tas
  • 214
  • 1
  • 3
  • 16

2 Answers2

0

In Activity A do this,

startActivity(new Intent(ActivityA.this, ActivityB.class));
    finish();

This will finish ActivityA when moving to Activity B.

Next, in ActivityB override the onBackPressed in your Activity class and use an intent to revert to the previous activity.

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

NOTE:

If you do not override onBackPressed then it will go to the Activity before ActivityA (if not available exit from the app).

Links that might help you on getting started with Activity lifecycle:

sanjeev
  • 1,664
  • 19
  • 35
  • But this code will cause to RAM overloading when I browse in the app about 1 hour, 30 activities etc. :( – K.tas May 31 '19 at 08:49
  • finish the activity when you are moving from activity A to activity B like you said and use this. – sanjeev May 31 '19 at 09:35
  • This is the simple activity passing to another activity. The question is not this. – K.tas May 31 '19 at 09:44
  • @K.tas yup. have you heard about `onBackPressed`? See my updated answer.. – sanjeev May 31 '19 at 11:15
  • "But this code will cause to RAM overloading when I browse in the app about 1 hour, 30 activities etc. :(" - explain how? There will be only one Activity running. – sanjeev May 31 '19 at 12:23
  • If I open an Activity and another one and another one at the end there will be many activities. For example, I am browsing on instagram for about 2 hours. open another person's profile page another one etc. if Instagram wouldn't close before one this would cause high memory usage. – K.tas May 31 '19 at 12:45
  • And that is why you call `finish()` to finish that particular activity.. – sanjeev Dec 02 '19 at 12:01
  • I solved this problem about 6 months ago. You can check what I mean here https://stackoverflow.com/questions/56393100/how-do-i-go-back-to-the-finished-activity-dynamically – K.tas Dec 02 '19 at 15:48
0
Intent i = new Intent(F.this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

or

Intent i = new Intent(F.this, A.class);
startActivity(i);
finish();
Markus G.
  • 1,620
  • 2
  • 25
  • 49
Chirag Rayani
  • 309
  • 1
  • 14
  • I want to keep track of before activity. Can you look at the question again? I don't want to simply pass one activity to another – K.tas May 31 '19 at 09:47
  • For example, user passed between activities A to B. Now finish A and pass B to C, then finish B. Then click to the back button and go B again. Because B is the activity that the user comes from to current(ActivityC) activity. – K.tas May 31 '19 at 09:51
  • If the user go to the C from A directly, then finish A. If press the back button, then goes to the A again. Because A is the activity that the user comes from to current(ActivityC) activity. – K.tas May 31 '19 at 09:54