Activity alias enabling with single top

880 Views Asked by At

In my app there is an activity with alias: MyActivity and MyAliasActivity registered in the Manifest. The purpose of the alias is to provide extra intent filter. This feature can be enabled/disabled in the runtime by the user. The activity and alias has got singleTop modifier.

However, if I start my app with the component disabled (MyActivity on the top), then I enable it, and broadcast the event matching the intent filter, MyActivity is created one more time (by the alias)- I've got two of them in the backstack which is undesired.

Can it he handled in any way? I need to have only one activity in the backstack.

1

There are 1 best solutions below

4
On BEST ANSWER

You should use singleTask instead of singleTop. singleTask will guarantee that you only have one instance of an Activity in the backStack. In case you start an Activity for the second time with singleTask launchMode, the onNewIntent will be ran instead of creating a new Activity. Another solution is to use singleInstance, this works pretty much the same as singleTask, keeping only one instance of Activity and running onNewIntent if it is already created.

For more information here is a good explanation of how it works:

https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en

Google reference:

https://developer.android.com/guide/topics/manifest/activity-element.html