I estimate that 90 percent of people working on A specific project will have A(login) — > B(home) — > A(login). What I want to make clear is that this isn’t that hard, and there are many ways to do it, I just wrote it the way I wrote it before, and another way. Of course there could be a better way, people can leave a message. thank you
The B(main interface) I’m talking about in general terms. For example, wechat, after logging in, first a main interface, but to < ME > – > < Settings > – > < exit button >. At this point, the B interface has second order depth. A home screen with four tabs, and then a Settings screen. There are two interfaces.
There are several requirements:
-
To exit the entire application, press the back button at A(login screen).
-
If A(login interface) – > B(main interface), you cannot directly return to A(login interface) on B(main interface). And double click on the B interface to return to exit the entire application. To exit the B and A screens.
-
After you click Logout on interface B, you need to return to interface A. In this case, pressing the back key on the A screen will exit the entire application.
My train of thought
-
The exit button in the B interface is one level deep and that’s too easy. A(login interface) – > B(main interface), A to finish, then B(main interface) – > A(login), start A(login interface), then B(main interface) to finish.
-
If you want to handle the exit button in the B screen as in the first case, then after B(main screen) — > A(login screen), start A(login screen), all the open screens should be closed. If there is A second level of depth, then close both activities. If there are more, they should all be shut down. Only A login interface is left.
At that time, I thought of this kind of trouble, so I thought of the way to start an Activity. If you set the startup mode of the Activity to “singleTask”, then when you start the Activity, all the activities above the Activity will be pushed out of the stack. So that leaves this one.
So I thought of setting the startup mode of A(login interface) as “singleTask”. Do not close A(the login screen) when logging in to B(the main screen), and then restart A(the login screen) when logging out, so that A(the login screen) shuts down all activities contained in B above. (PS: Here set A as “singleTask” and there will be another pit behind it, which will be explained later)
Ok, let’s take it one step at a time:
The first step
So let’s set this A in androidmanifest.xml to “singleTask”
<activity
android:name=".modules.view.activity.LoginActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>Copy the code
Since we want to startA at the bottom of the stack again when we log out, we do not call finish() when we call startActivity in Activity A.
The second step
We’ve already launched B(the main screen), and if you click the back button, you’re back to A, and that’s A problem. On the main screen, click Return and the message “Please click return twice to exit the application” will be displayed. I think we’re all home cooked. Override the onKeyDown method. Implement your own method:
long SystemTime = 0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
if(System.currentTimeMillis()- SystemTime < 1000&& SystemTime ! =0){
Intent intent = new Intent();
intent.setAction("ExitApp");
intent.setClass(HomePageActivity.this,LoginActivity.class);
startActivity(intent);
}else{
SystemTime = System.currentTimeMillis();
Toast.makeText(aty, "Double click the back button to exit.", Toast.LENGTH_SHORT).show(); }}return false;
}Copy the code
At this point, everyone noticed that I not only restarted the Activity A(login screen). Intnet intent.setAction(“ExitApp”); Start A(login screen) and finish() the Activity from A(login screen). When logging out, simply start the Activity A(login interface), so I need to make A mark to determine whether the Activity A(login interface) is’ close the application ‘or’ log out ‘.
The third step
The corresponding clicking event of “Log out” button also starts the Activity A(login interface).
Intent intent = new Intent(a);intent.setClass(getActivity(),LoginActivity.class);
startActivity(intent);Copy the code
The fourth step
Then we know, restart startActivity an already started Activity that starts in “singleTask” mode. The onNewIntent() method is called. So we replicate the onNewIntent() method in A(login screen)
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if ("ExitApp".equals(intent.getAction())) { finish(); }}Copy the code
All right, this is about to open up another big hole. Do not know if you have encountered, please pay attention to the front high energy !!!!!
As written above, we tested it and found no problem. Perfect realization of our requirements, and then at the time of B(main interface), I pressed the home button to return to the desktop, and then I click the icon of this app to enter, instead of pressing the Menu key to select this app in the background list to enter, you will find that B(main interface) is missing !!!! , changed to A(login interface) !!!! And if it is still the same as before, press the home button to return to the desktop at the time of B(main interface), press the Menu button to select the APP in the background list to enter, there is no problem !!!!
The cause of the problem was later discovered. Because our A is the login interface, the login interface must be the startup page in general, and the startup mode of the startup page is directly set to “singleTask”. At this time, after you minimize the application and click the desktop icon to enter again, you will return to the startup interface.
StartActivity = “singleTask”; startActivity = “singleTop”; startActivity = “singleTask”; startActivity = “singleTask”; startActivity = “singleTop”;
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);Copy the code
Then OK !!!!
Postscript: I think RxBus works fine. Subscribe to an event that says Finish () when each screen starts. Click the “Log out” button and send it to all subscribed activities. But I didn’t write it myself. Ha ha.