0

I have a scenario like:- There are three Ativities A, B, C.

1. A's launch mode is 'singleTask' and C's launchmode is 'singleTop' in manifest. A calls B and then B calls C.

2. Now C is the top most activity of the application stack.

3. Now Activity C is put to background by presseing device home key.

4. I have a service running which Sends broadcast for every say 15 mins.

5. In broadcast receiver i have an intent which is like this

Intent i = new Intent(context, A.class); 
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

Now Activity 'A' comes in foreground and in its onResume() i call Activity 'C' as ACTIVITY_RESET_TASK_IF_NEEDED (have some flag that tells me that Activity 'C' was invoked alreday, so lets bring it in foreground. If that flag is set to false then it would never invoke Activity 'C' directly rather Activity 'B' will get called and then 'B' will call Activity 'C').

What i trying to achieve here is the user perspection that when Activity 'C' was running then that Activity 'C' should come in foreground.

Here is the Problem now:- I never get the same Actvity 'C' instance due to FLAG_ACTIVITY_NEW_TASK and thus my app goes in wierd state. But when my app is put to background using device back key and i exceute all those steps it works i.e i get the same instance of Activity 'C' (Don't know how android is handling it internally this, but in both the flows home and back key there is not consistency).

So how do i achieve or maintain my app state in case for home key being pressed..?

Help Appreciated.

user755499
  • 2,241
  • 4
  • 25
  • 34
  • possible duplicate of [How to bring an activity to foreground (top of stack)?](http://stackoverflow.com/questions/2232238/how-to-bring-an-activity-to-foreground-top-of-stack) – Ted Hopp Dec 12 '11 at 06:25

1 Answers1

0

Hi use following flags while launching the Activity

Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

This flags insures that only allow one instance of your Activity if its already running then it has been brought to front from the stack if its not running then a new Activity is created.

ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
  • For this to work properly remove all other flags from from your C activity – ingsaurabh Dec 12 '11 at 06:28
  • I guess the problem is Activity 'A' gets called from FLAG_ACTIVITY_NEW_TASK and so Activity 'C' gets created in new task. So what you are trying to say is though its a new task, if activity was running then Intent.FLAG_ACTIVITY_REORDER_TO_FRONT would work right..? – user755499 Dec 12 '11 at 06:31
  • I clearly mentioned what the above flag does now you have to change your flags accordingly of related Activity BTW the functioning you are asking for can be achieved by above flag also remove this flag from C FLAG_ACTIVITY_NEW_TASK – ingsaurabh Dec 12 '11 at 06:33
  • FLAG_ACTIVITY_NEW_TASK flag is set for Activity 'A' which is needed because it gets called from broadcast receiver and yes i will try to remove other flags for Activity 'C' and will add only FLAG_ACTIVITY_REORDER_TO_FRONT for it. – user755499 Dec 12 '11 at 06:39