How to prevent the app from running in the background, click on the app Desktop icon to restart?
In the project, encountered a problem baffled, that is: I in the app use process, click the Home button, and then go to look at other applications, this time then click on the App Desktop icon, this time the app is restarted, and not from the last stop interface started.
For the above situation, I think since my app is already running in the background, why can't continue to re-run the interface, I have to run new. Then I went to check the information to solve the problem. Let's start with the nature of the phenomenon.
Why: When you click on the app Desktop icon, the app defaults to the task you want to create a new app instead of judging the same apps you have running in the background.
After practice I found: When you click on the app Desktop icon, the app will re-create your app's launch page, however, you can quickly click the Back button and you will find that you will go back to the interface on the previous exit. After reviewing the data, the system will record the stack of the boot order of the acitivity. And put the current startup page at the top, as shown in:
Note: The information above says that the previously activated activity is not, but the system records their boot order, but you press the Back button, the system will automatically re-create new activity, Add when the app started 1 to 11 of activity, however, in 11 this activity, you clicked on the Home button, or click on other software such as QQ, this time your app into the background, 1 to 11 of these activity is actually collected by the system, but The system records this activity boot sequence stack , and then when you go back to the application, the system is actually re-created ACTIVITY11, and then click the Back button, right re-create the ACTIVITY10, this is the reverse creation of activity principle.
However, when you put the app in the background, this time click on the App Desktop Launcher icon, this time the system will default you to open a new app, but because a software can only run on the phone, so the system found that your previous app is still in the background, This time the system will put the newly created activity on top of the previous activity stack, as shown in the Activity1
Having known the reason, we were ready to deal with it.
First step: Check the startup mode of the Activity1 if the Activity1 startup mode is Singletask
Android : launchmode= "Singletask"
then you have to remove him or change to "standard".
Step Two: Set under the Application tab of your app's Androidmanifest.xml file
Android :p ersistent= "true"
persistent to true to prevent your app from being recycled in the background.
Step Three: Set the following method in the Activity1 OnCreate method:
@Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); if (Getintent (). GetFlags () & intent.flag_activity_brought_to_front)! = 0) { finish (); return; } Setcontentview (r.layout.activity1_layout); Regular Activity Creation Code ... }
In fact, the following code is set before Setcontentview ():
if (Getintent (). GetFlags () & intent.flag_activity_brought_to_front)! = 0) { finish (); return; }
Used to determine the activation flag for this activity, to see if the app is running from the background to the foreground. If it is, then directly to the finish () off, and then the system will go to the activity to start the history stack query on an activity, and then create new it, so we have to press the home button to go out of the interface. Okay, that's it. The app is running in the background and clicking on the app icon will restart the issue. Hope to help you!
How to prevent the app from running in the background, click the App Desktop icon to restart?