Android programming a method for exiting an entire application

  • 2020-11-26 18:59:16
  • OfStack

This article gives an example of how Android programming exits the entire application. To share for your reference, the details are as follows:

When writing android applications, we often run into situations where we want to exit the current Acitivity or simply exit the application. My previous one was either to press the back key or to press home to return, neither of which actually closed the current application or released system resources. Sometimes the jump of activity is too large, and you need to press the back key many times, which makes 1 point uncomfortable.

This method can only use system.exit (0) to close the active Activity. The code is as follows:


public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuItem item = menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "Exit");
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
System.exit(0);
return true;
}
});
return true;
}

I think this is too inconvenient. Every time I quit the interface, the application is still running. Today, I checked the data and finally solved this problem.

There are several methods available on the web. I took what I thought was a simple and easy method, copied the following SysApplication class into the project, and then used SysApplication.getInstance ().addActivity (this) in each Acitivity oncreate method. Add the current Acitivity to ancivitylist and call SysApplication.getInstance ().exit () when you want to exit. You can simply close all Acitivity and exit the application.

The attached code:


import java.util.LinkedList; 
import java.util.List; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Application; 
import android.content.DialogInterface; 
import android.content.Intent; 
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; 
  } 
  // add Activity 
  public void addActivity(Activity activity) { 
    mList.add(activity); 
  } 
  public void exit() { 
    try { 
      for (Activity activity : mList) { 
        if (activity != null) 
          activity.finish(); 
      } 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } finally { 
      System.exit(0); 
    } 
  } 
  public void onLowMemory() { 
    super.onLowMemory();   
    System.gc(); 
  } 
}

Add activity to oncreate in the application

SysApplication.getInstance().addActivity(this)

Such as:


public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SysApplication.getInstance().addActivity(this); 
}

I hope this article has been helpful in Android programming.


Related articles: