0

I want to resume activity in below situation,

Activity A calls Activity B Activity B calls Activity C

now i want to go to Activity A but not to create new one use existing instance.

I've tried with Intent.FLAG_ACTIVITY_REORDER_TO_FRONT but it only reorder and if i press back i'm on Activity C where i don't want to be.

T want to clear Activity B and Activity C while starting Activity A.

please help...

VendettaDroid
  • 3,131
  • 2
  • 28
  • 41
MobileEvangelist
  • 2,583
  • 1
  • 25
  • 36

4 Answers4

1

Use FLAG_ACTIVITY_CLEAR_TOP flag

Dmytro Danylyk
  • 19,684
  • 11
  • 62
  • 68
  • It actually restarting that activity calling onCreate() method that ti don't want to be call when i fire intent for Activity A from C. – MobileEvangelist Sep 08 '12 at 09:53
0

You can make your activity A SingleInstance by declaring the launchMode in manifest file, so everytime the activity wont be created.

Whice you calling activity B and C use Flag NO_HISTORY with the intent. so when you will press back key they won't be on stack.


Read Intent - Intent Flags - Manifest file from developer doc.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • it receives data from intent such ad id to display details so, it can't be single instance .. – MobileEvangelist Sep 08 '12 at 09:59
  • You want : "Activity A but not to create new one use existing instance." – MKJParekh Sep 08 '12 at 10:06
  • I've tried this FLAG_ACTIVITY_NO_HISTORY but when i got from second i.e. Activity B to Activity C It's crashing. i don't knw y – MobileEvangelist Sep 08 '12 at 12:24
  • @MKJParekh Please don't recommend `singleInstance` launch mode. It is intended for HOME-screen replacements only. it usually creates more problems than it solves. In this case the OP just wants to clear the activity stack. This can be easily done with Intent flags. – David Wasser Sep 10 '12 at 07:32
  • @DavidWasser Okay, will surely update my self and will try to read more regarding that..thanks.If you suggest me then I can also remove this answer.. – MKJParekh Sep 10 '12 at 09:01
0

You should familiarize yourself with the activity lifecycle As well as the Activity class As well as the Bundle class and This local discussion on the subject which includes sample code. Using SingleInstance can help but you are at risk of the OS killing your app while in the background. Saving data before it closes will allow you to rebuild the state the app was in when the user put it in the background by advancing to one of your other activities or a new unrelated app (like if the phone was to ring).

Community
  • 1
  • 1
gh.
  • 339
  • 1
  • 3
  • 20
  • For some reason I was not allowed to post the answer with links. [local discussion](http://stackoverflow.com/questions/151777/saving-activity-state-in-android) [Bundle](http://developer.android.com/reference/android/os/Bundle.html) [Activity](http://developer.android.com/reference/android/app/Activity.html) [lifecycle](http://developer.android.com/training/basics/activity-lifecycle/index.html) – gh. Sep 08 '12 at 10:13
  • @Jay You can override the lifecycle methods to preserve data in your app so that when it is resumed it will be exactly as it was when the user left it. If done correctly, if the activity is paused in the background it will be brought to the front rather than relaunched, and if it has been killed while paused in the background, it will be restarted and the previous state can be rebuilt without the user having to do anything extra. Setting the [launchmode](http://developer.android.com/guide/topics/manifest/activity-element.html#lmode) and save your Bundle. Read the links I have posted above. – gh. Sep 08 '12 at 22:21
0

You need to set FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP to have activities B and C removed from the stack and to return to the existing instance of activity A. Like this:

Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
David Wasser
  • 93,459
  • 16
  • 209
  • 274