I tried solution two, but the problem is that one action cannot determine which startup icon was used (please tell me if I'm wrong).
My solution was to add two “dummy” actions, which then trigger the main action, which matches the correct page number. The difficulty with this approach is to properly handle the task stack. When a trigger is selected, it is necessary to start an empty activity, and it should send the intention to the main action. Android is trying very hard to stop you from doing this, and just bring the latest activity to the fore.
This is the best I could come up with:
Fictitious actions (similar to LaunchActivity2):
public class LaunchActivity1 extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent newIntent = new Intent(this, MainActivity.class); newIntent.putExtra(MainActivity.EXTRA_PAGE, 1); newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(newIntent); finish(); } }
In the main action:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewPager viewPager = (ViewPager)findViewById(R.id.MainViewPager); viewPager.setAdapter(new MyAdapter(getSupportFragmentManager())); int page = getIntent().getIntExtra(EXTRA_PAGE, -1); if(page >= 0 && page <= NUM_ITEMS) viewPager.setCurrentItem(page); } public void onNewIntent(Intent intent) { if(intent.hasExtra(EXTRA_PAGE)) { int page = intent.getIntExtra(EXTRA_PAGE, -1); ViewPager viewPager = (ViewPager)findViewById(R.id.MainViewPager); if(page >= 0 && page <= NUM_ITEMS) viewPager.setCurrentItem(page); } }
AndroidManifest.xml:
<activity android:name=".LaunchActivity1" android:label="Launch 1" android:clearTaskOnLaunch="true" android:noHistory="true" android:taskAffinity="Launch1" android:theme="@android:style/Theme.Translucent.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".MainActivity" android:label="My App" android:clearTaskOnLaunch="true" android:finishOnTaskLaunch="true" android:launchMode="singleTask" android:taskAffinity="MyApp"> </activity>
The problem with different task similarities is that both launchers, as well as the main task, appear in the Recent Applications list.
I would not recommend this approach to anyone else - instead, just use the single launch icon.
Raalf source share