2

I'm trying to resume an activity from within a broadcast receiver's onReceive() method as follows:

Intent i = new Intent(context, TimerSet.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

However the activity (TimerSet.class) is recreated instead of resumed. The only recommended solution I found to this problem was to use the FLAG_ACTIVITY_REORDER_TO_FRONT but I'm already using it.

Also, using Intent.FLAG_ACTIVITY_NEW_TASK doesn't fit my use case but I get the following exception when I do not provide it:

android:util.AndroidRuntimeException: Calling startActivity() from outside of an 
Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you 
want?
ionicom
  • 153
  • 2
  • 8
  • See this question http://stackoverflow.com/q/3689581/599614 – sstn Feb 18 '13 at 06:13
  • I don't see how that answers my question. – ionicom Feb 18 '13 at 13:07
  • @NickMaraston could you find any way for using FLAG_ACTIVITY_REORDER_TO_FRONT? I can use it for newer versions of Android. But for sdk 16 it has problem.. it errors me needed new task flag.. It's ridicules. – atasoyh Mar 28 '17 at 10:50

2 Answers2

4

I am not sure whether this is exactly your problem or not, but I have a situation where I got a notification and I want to start my app without starting a new instance (if it's already running) I finally figured out that these will work. The FLAG_ACTIVITY_NEW_TASK will not start a new instant if the activity has already been running. However, it will add it to the existing stack. Therefore, we can do a FLAG_ACTIVITY_CLEAR_TOP, so back will bring user to the home screen but not the previous state.

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
DaiLak
  • 746
  • 7
  • 10
1

remove FLAG_ACTIVITY_NEW_TASK flag. also add this flag ->FLAG_ACTIVITY_CLEAR_TOP. This would prevent new activity to be created if already present.

Fahad Ishaque
  • 1,916
  • 1
  • 17
  • 21
  • The exception persists upon removing the flag `FLAG_ACTIVITY_NEW_TASK`. – ionicom Feb 18 '13 at 13:08
  • 1
    just then simply add lag ->FLAG_ACTIVITY_CLEAR_TOP. let the previous flag stay. this worked for me. Else if you want your activity to be singleton, there's another solution to that also. – Fahad Ishaque Feb 18 '13 at 13:46