1

When I develop an Android application, how to exit this application? I try anything what I can do ,but I can't exit my application what I am developing. How to do this ? Any idea?

This my solution ,as folow,

public class SysApplication extends Application {
private List<Activity> mList = new LinkedList<Activity>();
private static SysApplication instance;

private SysApplication() {
}

public synchronized static SysApplication getInstance() {
    if (null == instance) {
        instance = new SysApplication();
    }
    return instance;
}

// When an activity is created  ,excute this method ,addActivity(activity) 
public void addActivity(Activity activity) {
    mList.add(activity);
}


      //When I want to exit this aplication ,excute this method ,exit()
public void exit() {
    for (Activity activity : mList) {
        activity.finish();
    }

    System.exit(0);
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    System.gc();
}

}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Davim
  • 221
  • 2
  • 7

2 Answers2

0

This can be useful for you : How to shutdown an android application?. There is method : Activity.finish() for closing activities.

Easy way is to press home button. It will put your app to the background and Android will take care of it.

Community
  • 1
  • 1
LadyWoodi
  • 486
  • 1
  • 5
  • 12
  • 2
    But hitting the home button won't close the app. It will just, as you told, leave Android manage it on the background, but nothing ensures you it is closing your application. I bet hitting the back button would do more for closing the app. However, if tied to code, `Activity.finish()` is the most accurate way to close the app. – Sergi Juanola Oct 17 '12 at 08:42
  • Yes Korcholis I agree. Maybe button with Finishing all running activities would be the best. I was just trying to show also the easiest way to exit app(not really closing it). – LadyWoodi Oct 17 '12 at 09:03
0

If you need an explicit exit event to, say, clean things up nicely, and you have a bunch of activities, services, etc., you may create BroadcastReceivers that do the cleanup job and, when your code decides it's time to "shut down" - be it a button or some logical conclusion - it should broadcast the corresponding intent.

You may need that, e.g., to prevent battery drain.

Otherwise, there seems to be no reason to care about "shutdown".

full.stack.ex
  • 1,747
  • 2
  • 11
  • 13