0

I have a lot of Activities on top of each other in my application and I want to close all these activities in one click. Can we do that. And another thing is I want my application to start fresh each time(dont want to run in background). How can I do that?

James
  • 13,891
  • 26
  • 68
  • 93

3 Answers3

1

There is no such thing as "closing" application or activities on Android. Please read: Is quitting an application frowned upon?

If you have a lot of instances of the same Activity class, then you might review your activity stack and task design.

Community
  • 1
  • 1
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
0

Not 100% sure there is a single android method that will close all of your related Activities but I think it might be possible if you use Broadcasts and broadcast receivers.

For example, if one of your Activities is currently 'on top' and the user closes it, the 'on top activity' could broadcast a close intent. all your other activities would have receivers listening for this activity and could then call finish() to end.

As for starting a fresh on each time this might be complicated. You'd have to look at overriding the 'home' and back buttons maybe but I highly advise against this. This is also complicated by the fact you want to achieve the above as well I think.

D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97
0

I believe you can do it with this:

@Override
        public void onClick(View v)
        {
            Context context = v.getContext();
            Intent intent = new Intent(context, Dashboard.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// This flag ensures all activities on top of the Dashboard are cleared.
            context.startActivity(intent);
        }

In this case, my Dashboard.class is the main opening activity of my application so its a way to take a user back to the start.

John J Smith
  • 11,435
  • 9
  • 53
  • 72