Android Acticity restart after restart and life cycle changes

First posted the code directly chon restart:

Intent intentReStrat = getIntent();
finish();
startActivity(intentReStrat);

It's that simple code is actually three lines, but when we actually use will find that there are some wrong, than if I did in onDestory such as the life cycle of the time, so we look at each marked Log Lifecycle

LogUtil.chunyLog().d("onCreat"+ this);

Activity in each of the life cycle have this log (plus behind this is to look at different Activity, restart because there is a new two-step process to generate a new and destruction)

OK, the rest is to get a button click event in the beginning of the reset code to write up

Add some to run. . .

First there will be the following:

First, you can see these go onCreat onStart onResume Needless to say, we see behind houm

@ 1ecbcf6f This is unique to this Activity

 

Next, click the button, restart Acticity:

Then we should seriously look at, and we put in front for the convenience of Activity (@ 1ecbcf6f) I said to Aty1  

Later, the new @ 2005b14f dang such as new Aty2 like to understand

OK, what happened next recovery

When we click the button, Aty1 go onPause first suspended

Then Aty2 gone onCreat onStart onResume three successfully created

Plus Aty1 gone down onStop onDestory was killed

When you can see the new creation has gone on Aty Aty of a onStop and onDestory that we will be here

 

Problem, each situation is different, because I was a binding Service,

In onDestory which calls inside a Service restore the default method, this will cause a problem when I restart Aty after the call is on a Aty of onDestory restore the default does not come in to do after performing a restart will become Aty thing, so do not

 

Solution:

Everyone solution is not the same, I was with the SP, click the button in time to save SP inside boolean variable (called the asd) saved as true

Then there is the method call Service onDestory out asd, if it is true does not call Service method, and the assignment false asd

Then perform the method exits else inside, so that we can simulate what

 

Aty come in time to exit asd is false (default) so go else call the Service exit to exit recovery method, if it is a click of a button, it is the first

asd saved as true then there would be no walking service restoration method, this is my solution, if there is a better, tell me what the trouble review progress together.

 

Published 124 original articles · won praise 141 · views 160 000 +

Guess you like

Origin blog.csdn.net/weixin_36838630/article/details/83622926