4

I have a broadcast reciever triggered by an alarm event, which checks if the application is being idle (user is not active). Now I would like to reset the activity stack and bring the application to the first / default activity.

But If I do something like this:

Intent intent = new Intent(context, StartUp.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP 
                | Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

then my application comes to the foreground.

I would like to do it silently so the user will not be disrupted by my application (as aparently he is doing something else).

So my question is how do I clear the activity stack without launching an activity?

Drejc
  • 14,196
  • 16
  • 71
  • 106
  • But if you want to clear the activity stack and not launch any activity all you want is to close your application! Otherwise what activity should be rendered when the user comes back. – Boris Strandjev Jan 20 '12 at 12:38
  • I assume if you clear the activity stack the default / first activity will be rendered. Closing the application is also an option, but then I need to know if any of my activities are showing on the screen. If not I can close the app, if yes I can use the intent as described in the question. – Drejc Jan 20 '12 at 12:47

2 Answers2

3

Checking whether any of your activities is on the foreground seems to be covered over here, though I have not tried it. Then if you want to terminate the whole application just do:

System.runFinalizersOnExit(true);
System.exit(0);

And you are done. Hope this will help you.

Community
  • 1
  • 1
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
0

On top of my head, why not save the state to for example the shared preferences and when user launches your application again you can detect it needs to be cleared and relaunch the activity. I.e. use a concept of delaying the reset of the stack. There are probably other implications such as SharedPreferences in BroadcastReceiver seems to not update?, just to give an idea.

Community
  • 1
  • 1
Daniel Backman
  • 5,121
  • 1
  • 32
  • 37