How to kill app when user taps the home key

Put your problem here if it does not fit any of the other categories.

How to kill app when user taps the home key

Postby Boomer » Thu Jun 03, 2010 7:48 pm

I have an application that is just about ready to go... if I can solve one last problem.

When the user taps the "home" key, as expected, the home screen shows up on the phone. However, when the use taps my application icon, the application opens up on the activity that was last viewed. I dunno, its seems the application is running in the background. I would like the app to restart on the first activity.

I considered using "finish()" in the onStop event, but that did not seem like right solution (although it may be and will I need to do it on all activities?).

Then I came across this while search the forum (credit to user rackham)...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ActivityManager aM = (ActivityManager) getApplicationContext().getSystemService( ACTIVITY_SERVICE );
  2. aM.restartPackage(getApplicationContext().getPackageName());
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Manifest.xml...
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.RESTART_PACKAGES" />
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


The above might be the solution I need. However, do I need to put the above on every activity.

Basically, I want the application to end when the user taps the home key no matter which activity they happen to be viewing.

Thanks in advance
Boomer
Freshman
Freshman
 
Posts: 5
Joined: Thu Jun 03, 2010 7:10 pm
Location: Honolulu, Hawaii

Top

Re: How to kill app when user taps the home key

Postby Boomer » Fri Jun 04, 2010 1:06 am

Update... I tried the second solution using the ActivityManager in the OnStop event, however, that also causes the app to shut down when tapping the back button. I have gone through the lifecycle docs, btw, however, I am still not sure how to solve my problem.

Any suggestions?
Boomer
Freshman
Freshman
 
Posts: 5
Joined: Thu Jun 03, 2010 7:10 pm
Location: Honolulu, Hawaii

Re: How to kill app when user taps the home key

Postby phix » Fri Jun 04, 2010 3:31 am

I asked a similar question a while back and the answer I got was something to the effect of "if you really want to start the app over, it's going to take a lot of ugly hacks in order to do so."

I never really had the need to start over completely, but I guess the answer that I was given would answer yours of "do I need to put this in each activity" to "Yes."

Sorry couldn't give you a concrete answer, but I haven't come across many apps that start from scratch each time.
Posting questions without code is like going to the doctor and saying "it hurts, fix it."
phix
Master Developer
Master Developer
 
Posts: 230
Joined: Mon Mar 15, 2010 10:37 am

Re: How to kill app when user taps the home key

Postby Boomer » Fri Jun 04, 2010 7:07 am

Thanks for the reply. I guess others have the same question. I notice that many of the apps close and open on its "starting" activity. I would really like to get my app to do the same.

Anyway, I tried putting the code on each of my activities in the onStop() event, but it also kill the app when using the back button, which I don't want. I only want to shut down when the user taps the home key and I would like the app to restart on the first activity.

Again, if there are any other suggestions I could use them.
Boomer
Freshman
Freshman
 
Posts: 5
Joined: Thu Jun 03, 2010 7:10 pm
Location: Honolulu, Hawaii

Re: How to kill app when user taps the home key

Postby Boomer » Fri Jun 04, 2010 9:15 am

Okay, I found solution to my problem. Basically, I wanted my app to restart at the first activity all the time. However, after tapping the home key on subsequent activity, my app would restart at the last viewed activity.

Ok, so I found that if I use the following attribute in the "activity" element in manifest.xml I got the behavior I was looking for...

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:clearTaskOnLaunch="true"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


Now my app always opens on my splash page when the user taps the app icon.
Boomer
Freshman
Freshman
 
Posts: 5
Joined: Thu Jun 03, 2010 7:10 pm
Location: Honolulu, Hawaii

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 19 guests