How to finish() an Activity when Home button pressed

…衆ロ難τιáo~ 提交于 2019-11-29 06:40:25
dylan murphy

what about

android:launchMode="singleTask"

or

android:launchMode="singleInstance"

in your manifest? i think singleTask is the one you want, but im still not crystal clear on what you are doing.

"The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one." singleTask

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

dev docs: Acitvity Lifecycle , Finish

Quang Long

Set android:clearTaskOnlaunch="true" on the activity launched from the home 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>

Not sure about finish() on home button press but i think you can finish() the previous activity using:

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

Probably not the best way of doing it though. I think you can also call the parent activity of a subactivity and finish it that way but not sure.

JDK

I had the problem with closing the sound on home button is pressed. I did this code below. Hope it wil help you. Override onpause() method.

 @Override
 public void onPause(){
      System.exit(0);
      super.onPause(); 
 }
@Override
public void onStop() {
    super.onDestroy();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!