Search code examples
androidandroid-manifestlauncher

How to open main activity from two separate icons with different ui


I have an app that the main activity is including a map. I want to create two icons on my home screen launcher, both of them will open the same main activity but with a different UI on the map.

For example : If I will press on icon A the app will be open with a fab on the map, and if I will press on icon B the app will be open without the fab on the map.


Solution

  • First you need to add second launcher intent to your manifest.

            <activity
            android:name=".yourpackage.MapActivity"
            android:launchMode="singleTask"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
    
            <meta-data android:name="visibility" android:value="0"/>
        </activity>
    
        <activity-alias
            android:name=".MapWithoutFabActivity"
            android:targetActivity=".yourpackage.MapActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
    
          <meta-data android:name="visibility" android:value="1"/>
        </activity-alias>
    

    Next we need modify our map MapActivity for being ready to change visibility of fab button.

    public class MapActivity extends AppCompatActivity {
    
       protected int fabVisibility = View.VISIBLE;
    
       @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
    
           // Hope this method works. 
           Bundle bundle = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA).metaData;
        int visibility = Integer.valueOf(bundle.getString("visibility"));
        fab.setVisibility(visibility);
       }
    
    protected void onNewIntent(Intent intent) {
           Bundle bundle = getPackageManager().getApplicationInfo(getPackageName(), 
           PackageManager.GET_META_DATA).metaData;
           int visibility = Integer.valueOf(bundle.getString("visibility"));
           fab.setVisibility(visibility);
    }
    

    Good luck there

    Emre