How to complete () an action when you click the Home button

For a complex reason, I need to be able to finish () my actions when the user presses the HOME button.

The story here is that I have a desktop widget that launches another part of my application that has completely transparent activity (so the desktop continues to show, even though my activity is running). If previous actions were interrupted using the "Home" button, they are brought to the fore and hide the main screen.

Or as an alternative, can I activate a new activity () of a previous activity?

+7
source share
5 answers

What about

android:launchMode="singleTask" 

or

 android:launchMode="singleInstance" 

in your manifest? I think singleTask is the one you want, but I still do not have a crystal clear understanding of what you are doing.

"The system creates activity at the root of the new task and redirects it to it. However, if an action instance already exists, the system redirects the intention to the existing instance by calling it onNewIntent () rather than creating a new one." singleTask

 @Override void onPause() { super.onPause(); finish(); } 

dev docs: Acitvity , Finish Life Cycle

+8
source

Set android: clearTaskOnlaunch = "true" in the action launched from the main screen. Example:

 <activity android:name="MainActivity" android:exported="true" android:clearTaskOnLaunch="true" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 
+3
source

Not sure if on the home () button on the "home" button, click, but I think you can finish () the previous action using:

 Intent i = new Intent(MainActivity.this, SecondActivity.class); startActivity(i); finish(); 

This is probably not the best way to do this. I think you can also name the parent activity of subactivity and end it this way, but you are not sure.

0
source

I had a problem with closing the sound on the home button. I made this code below. I hope this helps you. Override the onpause () method.

  @Override public void onPause(){ System.exit(0); super.onPause(); } 
0
source
 @Override public void onStop() { super.onDestroy(); } 
0
source

All Articles