0

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.

  • Just handle visibility of Fab for GONE and VISIBLE on different icon press or you can make two fragment in one activity and launch according to your need on icon press – Ashish Garg Jun 19 '17 at 14:35
  • try checking this out https://developer.android.com/guide/topics/ui/shortcuts.html seems to be what you want, but it is for the latest versions I think – Cruces Jun 19 '17 at 14:41
  • I don't know if this works, but this also seems to be what you need https://stackoverflow.com/questions/43361498/android-quick-shortcuts-passing-intent-extraor-some-data-in-shortcuts-xml – Cruces Jun 19 '17 at 14:44
  • I think I explained myself not so well. I want different icons on my home screen launcher, I want to change the UI before I will get inside the app. The fab was just an example. And I use api 21 so I don't have shortcuts – Eitamar Saraf Jun 19 '17 at 14:44
  • 1
    I never saw an app with two icons. Doesn't seem to be a good practice. Why do you need 2 launchers icons? – Eselfar Jun 19 '17 at 14:54
  • The app have two working mods , so the app needs to remain the same but with other ui – Eitamar Saraf Jun 19 '17 at 14:57

1 Answers1

4

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

Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
  • Fixed some fails. Please check again – Emre Aktürk Jun 19 '17 at 14:50
  • If the app is already opened, it's restarted from the previous screen. Means if you open the app with the icon A, then if you click on B the screen A is displayed as the app displays its previous state. – Eselfar Jun 19 '17 at 14:53
  • They will both going to be opened as stacked. They are independent. – Emre Aktürk Jun 19 '17 at 14:55
  • Yeah I thought about that. But the problem is that we don't want to implement another activity, we want to open the same map activity just change the UI on the map – Eitamar Saraf Jun 19 '17 at 14:55
  • You can override launchMode in your manifest for your activity. Then override onNewIntent method to handle. – Emre Aktürk Jun 19 '17 at 14:56
  • But i am not sure how you are going to add second launcher that respects to same activity within data. – Emre Aktürk Jun 19 '17 at 14:58
  • @EmreAktürk So basically you set `android:launchMode="singleTask"` on both Activities? – Eselfar Jun 19 '17 at 15:02
  • If we want change same activity then we need to remove other one. I m editing now. – Emre Aktürk Jun 19 '17 at 15:03
  • @Eitamar Saraf i have edited my answer. As you can see i just added metadata to manifest for each launcher. So we need to get it when our activity starts and behaive up to it. Also added singleTask so each time that same activity is going to be opened it will call onNewIntent in same activity. Also removed other layered activity. These 0 and 1 values in metada are the same with View.VISIBLE and View.INVISIBLE. You can change it to View.GONE if you want. There is also a tag called activity-alias for manifest but i am not sure how to use it. You can check documantation for it. – Emre Aktürk Jun 19 '17 at 15:11
  • 1
    @EmreAkturk is it possible to give the same activity twice – Eitamar Saraf Jun 19 '17 at 15:20
  • So that we can use activity-alias. Gonna edit my answer again :) – Emre Aktürk Jun 19 '17 at 15:29
  • @EitamarSaraf I have used activity-alias for duplication. – Emre Aktürk Jun 19 '17 at 15:31
  • @EmreAkturk I tried your solution , has you suspected I just needed to change getApplicationInfo to getActivityInfo and wrap it with try and catch – Eitamar Saraf Jun 21 '17 at 06:22