Android Homescreen Launcher App Development - Previous launcher shows behind my transparent launcher activity

110 Views Asked by At

I'm developing an Android launcher homescreen app, and whenever I go to set the application I'm developing as the default launcher, it shows my launcher over the previously set default launcher on the homescreen.

I want to use the system background, so my homescreen app's main activity has a transparent background, but I don't want it sitting on top of the previous launcher's UI (OneUI).

Is there an activity tag that should be in my application manifest, or something to ensure the previously set launcher is no longer running? Thanks for any help!

example outcome

I've looked into how I can close other tasks on Android, but my app doesn't have the permissions. I'm not sure how else to remove other apps from the UI under my transparent default launcher within the scope of its permissions.

This is the current activity in the manifest:

<activity
   android:launchMode="singleTask"
   android:name=".MainActivity"
   android:exported="true">
   <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.HOME" />
   </intent-filter>
</activity>
1

There are 1 best solutions below

0
Andrew Orobator On

To set your application as the default launcher and ensure that it replaces the previously set launcher on the home screen, you can make use of the CLEAR_TOP flag in the Intent used to start your main activity. This flag clears the activity stack of the target application, removing any previously opened activities.

Here's an example of how you can modify your MainActivity to achieve this:

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   // Check if the application is already the default launcher
   if (!isMyLauncherDefault()) {
       // Create an intent with the CLEAR_TOP flag
       Intent intent = new Intent(Intent.ACTION_MAIN);
       intent.addCategory(Intent.CATEGORY_HOME);
       intent.addCategory(Intent.CATEGORY_DEFAULT);
       intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

       // Start the intent
       startActivity(intent);
       finish();
   }
   // Rest of your onCreate code...
}

The isMyLauncherDefault() method is used to check if your application is already set as the default launcher. You can implement it as follows:

private boolean isMyLauncherDefault() {
   String packageName = getPackageName();
   String defaultLauncher = null;

   Intent intent = new Intent(Intent.ACTION_MAIN);
   intent.addCategory(Intent.CATEGORY_HOME);

   ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
   if (resolveInfo != null && resolveInfo.activityInfo != null) {
       defaultLauncher = resolveInfo.activityInfo.packageName;
   }

   return defaultLauncher != null && defaultLauncher.equals(packageName);
}

This code will check if your application is already the default launcher. If it's not, it will create an Intent with the CLEAR_TOP flag and start the intent to bring your launcher to the front, effectively replacing the previous launcher. The finish() method is called to ensure that the previous launcher's activities are removed from the stack.

Remember to replace MainActivity with the correct name of your launcher's main activity class.

By using this approach, your launcher will be set as the default and will replace the previous launcher on the home screen.