Finish and killprocess use the required _ one app two activity

Source: Internet
Author: User

Click Finish and killprocess to use the required _ one app onr activity!


This is the case now. Service (the code is still in the previous article. If it hasn't changed, it won't be pasted here !) Check the task content in the background. The other project has two activities (taskdemoactivity and secondactivity) to start the project and then the taskdemoactivity starts the secondactivity.


The layout interface of the two activities is very simple. It is a button. The Code of taskdemoactivity is similar to the previous one.

Taskdemoactivity code is not detailed. The only change is the button event, as shown below:


Public void ongo (view ){
Intent intent = new intent (taskdemoactivity. This, secondactivity. Class );
Startactivity (intent );
}


The rest are several callback methods, and then print their information!


Secondactivity code has nothing to do, but it is attached to the following for convenience.


Package mark. Zhang. Demo;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. view;

Public class secondactivity extends activity {

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Second );
Log. D ("mark", "secondactivity = oncreate ()");
}

@ Override
Protected void onresume (){
Super. onresume ();
Log. D ("mark", "secondactivity = onresume ()");
}

@ Override
Protected void onstart (){
Super. onstart ();
Log. D ("mark", "secondactivity = onstart ()");
}

@ Override
Protected void onrestart (){
Super. onrestart ();
Log. D ("mark", "secondactivity = onrestart ()");
}

@ Override
Protected void onpause (){
Super. onpause ();
Log. D ("mark", "secondactivity = onpause ()");
}

@ Override
Protected void onstop (){
Super. onstop ();
Log. D ("mark", "secondactivity = onstop ()");
}

@ Override
Protected void ondestroy (){
Super. ondestroy ();
Log. D ("mark", "secondactivity = ondestroy ()");
}

Public void onover (view ){
// To be continued
}
}


Okay, a lot of nonsense. Now we will start working officially.


First test:


Public void onover (view ){
Android. OS. process. killprocess (Android. OS. process. mypid ());
}


The service detects that taskdemoactivity still exists in the task. This indicates that the killprocess method cannot end the entire app, but only the activity itself (secondactivity ).


Second test:


@ Override
Protected void ondestroy (){
Super. ondestroy ();
Log. D ("mark", "secondactivity = ondestroy ()");
Android. OS. process. killprocess (Android. OS. process. mypid ());
}

Public void onover (view ){
Finish ();
}


First, let's take a look at taskdemmoactivity's lifecycle changes (taskdemoactivity --- click the button --- secondactivity --- click the button ):


D/mark (367): taskdemoactivity === oncreate ()
D/mark (367): taskdemoactivity === onstart ()
D/mark (367): taskdemoactivity === onresume ()
D/mark (367): taskdemoactivity === onpause ()
D/mark (367): taskdemoactivity === onstop ()
D/mark (367): taskdemoactivity === onrestart ()
D/mark (367): taskdemoactivity === onstart ()
D/mark (367): taskdemoactivity === onresume ()


It can be seen that taskdemoactivity does not call ondestroy, indicating that this app has not been completed. However, the result is that taskdemoactivity is not in the foreground and is directly returned to the home page.


The most depressing thing is that the service will detect the existence of the activity for a long time. After 3-4 s, the service will not detect taskdemoactivity! It is reasonable to say that taskdemoactivity will be there all the time. Why will it be absent after a while?


If you start this app, you will find that taskdemoactivity will be like this:


D/mark (367): taskdemoactivity === oncreate ()
D/mark (367): taskdemoactivity === onstart ()
D/mark (367): taskdemoactivity === onresume ()


Then, the life cycle of secondactivity changes:


D/mark (367): secondactivity === oncreate ()
D/mark (367): secondactivity === onstart ()
D/mark (367): secondactivity === onresume ()
D/mark (367): secondactivity === onpause ()
D/mark (367): secondactivity === onstop ()
D/mark (367): secondactivity === ondestroy ()


We can see that secondactivity is over!


This indicates that android. OS. process. killprocess (Android. OS. process. mypid () is unreliable! Suspect that you have a bad father!


If your app design encounters similar problems, please test it carefully!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.