An attempt to exit the APP perfectly and exit the app
This demo will be made mainly because the group is talking about this issue after work, and the interest starts from the very beginning. After work, I will continue to work overtime to try a demo. I don't need to talk about it anymore. I used to write my school compositions like this. Let's start with a long word.
Of course, when talking about exit of the perfect APP, I used to use List to store every Activity. When I quit, I want to disable all the activities, you can call finishAll directly. It is actually an ActivityManager manager. But... But... Someone asked this question in the group today. The group owner said that he used this method to exit the application, not the people in my group. In an instant, I felt that I was too low to paralyze people who were not so low. I was also shot while lying down. I did this all !!! Therefore, Xing (1) Qu (QI) Yi (zhi) Lai (below) started a demo and checked the online materials. It seems that there are two kinds of comparisons that are acceptable, it is also a perfect way to exit. The following describes the implementation methods ..
I. Send broadcast quit (as many people know)
This exit method has been heard for a long time, but I did not try it. I searched the internet today and implemented it. It is indeed OK. It is recommended.
The specific code is as follows: first create a base class of the Activity as the place to put Broadcast, so that other activities only need to inherit this base class, you do not have to rewrite the registration and logout broadcast every time.
The Code is as follows:
public class BaseActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);IntentFilter filter = new IntentFilter();filter.addAction("com.jrue.exitApp");registerReceiver(mExitReceiver, filter);}private BroadcastReceiver mExitReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {if ("com.jrue.exitApp".equals(intent.getAction())) {context.unregisterReceiver(this);finish();}}};}
When you exit, you only need to send the broadcast:
case R.id.exit_use_broadcast:Intent intent = new Intent("com.jrue.exitApp");getApplicationContext().sendBroadcast(intent); break;
Good! This method works perfectly.
II. Switch to the background, and finally finish (I don't know how to name it)
This is probably the case. I don't know if I will understand it correctly. It is changed to Intent through setFlags. FLAG_ACTIVITY_CLEAR_TOP status. How can this status be detailed? If the Activity to be entered already exists in the stack, the other Activity on the Activity will be removed from the stack, to move the target Activity to the top of the stack. Then setAction's Intent. ACTION_MAIN finds the first Activity of the program, that is, the Activity that the program opened for the first time. It is likely to be the Activity at the bottom of the stack, so the purpose of doing so is very simple.
To sum up, we intentionally put an application in to open the first opened activity, and then change the Flags so that the Activity on the first Activity goes out of the stack, so that the first Activity reaches the top of the stack, then finish () directly, because there is only one Activity in the stack, and finish is to close the application. This method is too cool.
The Code is as follows:
Intent intent2 = new Intent(Intent.ACTION_MAIN); intent2.addCategory(Intent.CATEGORY_HOME); intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent2); // android.os.Process.killProcess(android.os.Process.myPid()); finish();
Okay. In this case, the demo will also be provided, which is useful and the best. If it is useless, you can ignore it and ignore it ~~~ Younger brother!
Download demo
Zookeeper
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.