3

So basically I'm stuck.... I want to switch between activities without closing the activities... For instance, "Activity 1" has the webpage Google and "Activity 2" has Facebook... How can I switch between both actives without the webpages closing and reopening??

David Wasser
  • 93,459
  • 16
  • 209
  • 274
Kezxi
  • 63
  • 6
  • can you post your activities so we can see what are you doing – hfarhanahmed Mar 21 '19 at 05:01
  • Just use `Intent`. Basically , if you use `Intent` to go to other Activity, previous Activity will stay at there until you call `Activity.this.finish();` . Or you can save your Activity state, (read ; https://stackoverflow.com/questions/151777/how-do-save-an-android-activity-state-using-save-instance-state) – MrX Mar 21 '19 at 05:03
  • try to look at Activity launchModes – Archie G. Quiñones Mar 21 '19 at 05:43
  • Even if you will use launchModes and you will not call finish() you are still not guaranteed that the system will not destroy activity which is not currently visible. I think you should consider using 2 fragments in one activity. – RadekJ Mar 21 '19 at 06:07
  • This answer might help you https://stackoverflow.com/questions/54967503/android-how-to-switch-to-activity-that-has-already-been-created/54967969#54967969. – Son Truong Mar 21 '19 at 07:37
  • impossible on certain vendors. – Shark Mar 21 '19 at 10:26

1 Answers1

2

to rearrange activites in the stack without opening and closing them, you can do the following (when launching an Activity):

Intent intent = new Intent(this, TargetActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

DO NOT call finish()!

This will look in the stack and see if an instance of TargetActivity already exists. If it does, it will simply be moved to the top of the stack (so that the user can see it). If no such instance exists in the stack, a new one will be created. So you don't need to know if an instance already exists or not.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Happy to help! If my answer was helpful you can "accept' it by clicking the green checkmark next to the answer. That will give us both some reputation points and get this question off the list of unanswered questions. It may also help others who search for a similar problem. – David Wasser Mar 24 '19 at 19:15