I am developing an Android app that has four activities: FormActivity, SplashActivity, MainActivity, and WebViewActivity. I want to set MainActivity as the home launcher, so that when the user presses the home button, it opens my app. I also want to provide an option for the user to change the default home launcher, so I created an intent to navigate the user to the home app settings.
The problem is that after the user sets my app as the default home launcher and then changes it back to the original one, my app always reopens the home app settings activity when launched, instead of MainActivity. This happens until I clear the settings activity from the task list.
Here is my manifest code:
<activity
android:name=".FormActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name=".SplashActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode"
android:noHistory="true">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity
android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode"
android:name=".WebViewActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
And here is my function that is called when the back button is pressed, which creates a new activity to navigate the user to the home app settings:
private void promptChangeHomeApp() {
PackageManager packageManager = getPackageManager();
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo defaultLauncher = packageManager.resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
String defaultLauncherPackageName = defaultLauncher.activityInfo.packageName;
if (defaultLauncherPackageName.equals(getPackageName())) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Change default home launcher");
builder.setMessage("It looks like the app has been set as your launcher home. Please click the 'Settings' below to remove this app from the launcher home and set the default launcher home as the default.")
.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_HOME_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
})
.setNegativeButton("Cancel", null);
AlertDialog alert = builder.create();
alert.show();
} else {
super.onBackPressed();
}
}
I have searched for similar questions on Stack Overflow, but none of them solved my problem. How can I fix this issue and make my app always open MainActivity when launched, regardless of the previous activity? Any help would be appreciated. Thank you.