I. Container Type
Create a global container, store all activities, and iterate through all activities when you exit
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; public class BaseActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Add Activity to the stackAtyContainer.getInstance().addActivity(this); } @Override protected void onDestroy() { super.onDestroy(); //End Activity - Remove the Activity from the stackAtyContainer.getInstance().removeActivity(this); } } class AtyContainer { private AtyContainer() { } private static AtyContainer instance = new AtyContainer(); private static List activityStack = new ArrayList(); public static AtyContainer getInstance() { return instance; } public void addActivity(Activity aty) { activityStack.add(aty); } public void removeActivity(Activity aty) { activityStack.remove(aty); } /** * End all Activities */ public void finishAllActivity() { for (int i = 0, size = activityStack.size(); i if (null != activityStack.get(i)) { activityStack.get(i).finish(); } } activityStack.clear(); } }
This method is relatively simple, but you can see that activityStack holds a strong reference to this Activity, that is, when an Activity abnormally exits, activityStack does not even release the reference, which can cause memory problems. Next, let's look at a similar way, but it's a little elegant.
II. Broadcasting
By registering a broadcast in BaseActivity and sending a broadcast when quitting, finish quits
public class BaseActivity extends Activity { private static final String EXITACTION = "action.exit"; private ExitReceiver exitReceiver = new ExitReceiver(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); IntentFilter filter = new IntentFilter(); filter.addAction(EXITACTION); registerReceiver(exitReceiver, filter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(exitReceiver); } class ExitReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { BaseActivity.this.finish(); } } }
III. PROGRESSIVE TYPE
By killing the process of the current application directly to end the application, simple and rough, and has (wu) effect!
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
manager.killBackgroundProcesses(getPackageName());
These three can achieve the same effect, but on the simulator will pop up Unfortunately, XXX has stopped message prompt box, but it can exit the application. Some of the real machines fail directly and only finish es the current activity (for example, the millet note in my hand, several domestic ROM fw layer changes too much, using this method needs to be cautious)
IV. RS Elegance
What is RS? Receiver+singleTask. We know that Activity has four loading modes, and singleTask is one of them. After using this mode, when startActivity, it first queries the current stack for the existence of an instance of Activity. If it exists, it moves it to the top of the stack and removes all activities above it. We open an app, first a splash page, and then finish off the splash page. Jump to the home page. Then N hops will be made on the home page, during which a variable number of activities will be generated, some will be destroyed, some will reside in the stack, but the bottom of the stack will always be our Home Activity. This makes the problem much simpler. We only need two steps to gracefully implement the exit of app.
1. Register an exit broadcast in HomeActivity, just like the second broadcast, but here you only need to register on a page of HomeActivity.
2. Set the startup mode of HomeActivity as singleTask.
When we need to quit, we just need to start Activity (this, HomeActivity, class) and send an exit broadcast. The above code first removes all activities above the HomeActivity from the stack, and then receives the broadcast finish itself. All OK! No bouncing boxes, no Rom adaptation. There will be no memory problems, it is so elegant, simple!
V. SingleTask Reformat
After communicating with some guys, many of them said that registering for broadcasting was a little troublesome. The guys downstairs put forward a simpler way of thinking.
1. Set the loading mode of MainActivity to singleTask
2. Rewriting the onNewIntent Method in MainActivity
3. Add an exit tag to Intent when you need to exit
Because many small partners are keen on source code, we will explain this way directly in the form of code.
The first step is to set the loading mode of MainActivity to singleTask
android:launchMode="singleTask"
The second step is to rewrite the onNewIntent() method
private static final String TAG_EXIT = "exit"; @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (intent != null) { boolean isExit = intent.getBooleanExtra(TAG_EXIT, false); if (isExit) { this.finish(); } } }
Step 3 Withdrawal
Intent intent = new Intent(this,MainActivity.class); intent.putExtra(MainActivity.TAG_EXIT, true); startActivity(intent);
6. Lazy Style
This approach is simpler, requiring only the following two steps
1. Set MainActivity to singleTask
2. Place exit in MainActivity
We can see that many applications exit applications by double-clicking the home key twice, which is based on this way. Here is how to deal with the source code private boolean mIsExit which exits by two consecutive clicks.
@Override /** * Double-click the return key to exit */ public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (mIsExit) { this.finish(); } else { Toast.makeText(this, "Re-press Exit", Toast.LENGTH_SHORT).show(); mIsExit = true; new Handler().postDelayed(new Runnable() { @Override public void run() { mIsExit = false; } }, 2000); } return true; } return super.onKeyDown(keyCode, event); }