Switch TargetActivity programatically
Switch TargetActivity programatically
I have an activity alias that leads to one of my activities.
<activity-alias
android:name="LauncherOne"
android:enabled="true"
android:targetActivity="org.test.app.LauncherActivity">
<intent-filter>
<action android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity-alias>
I want to be able to choose which activity it goes to, so how can I change the targetActivity programatically?
I can't really find a workaround, because I cant enable/disable two different targetactivities as it would forget the default launcher every time.
And I haven't found a way to switch acitivities that doesn't cost some performance in my app.
1 Answer
1
I want to be able to choose which activity it goes to, so how can I change the targetActivity programatically?
You can't. That information is baked into the manifest and cannot be changed at runtime.
Workaround: Have one activity that is your launcher activity. Have that activity use Theme.Translucent.NoTitleBar as a theme, so it has no UI. Have that activity launch the "real" activity based on whatever criteria you choose, then finish() itself so that it is no longer in the back stack. Roughly speaking, this is the same approach that would be used by an app that had a splash screen activity that then decided what activity to go to next, just without the actual splash screen UI.
Theme.Translucent.NoTitleBar
finish()
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.