Android:动态选择启动活动并不总是有效
我在这里(和其他地方)阅读了一些文章,这些文章描述了如何在启动应用程序时动态选择要显示的活动。下面是我的代码:
AndroidManifest.xml
<activity android:name=".StartupActivity"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
StartupActivity.java
public class StartupActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent;
if (RandomClass.getSomeStaticBoolean())
{
intent = new Intent(this, ActivityOften.class);
}
else
{
intent = new Intent(this, ActivityRare.class);
}
startActivity(intent);
finish();
}
}
ActivityOften 和 ActivityRare 都在清单中声明(当然没有启动器类别)并分别扩展 ListActivity 和 Activity。 99% 的情况下,显示的第一个活动是基于 RandomClass.getSomeStaticBoolean() 的 ActivityOften。
因此,第一次从图标启动我的应用程序时,我进入了 StartupActivity.onCreate。选择是正确的。但随后任何启动应用程序的尝试(从快捷方式或应用程序菜单)都会再次显示 ActivityOften。 StartupActivity 类内不再发生中断。尽管我知道 RandomClass.getSomeStaticBoolean() 已更改值并且 ActivityRare 应该出现,但第一个活动不断弹出。
有什么想法吗?
谢谢,谢谢,谢谢,丹克,谢谢! 肖恩
I've read a few articles here (and other places) that describe how to dynamically choose which activity to show when launching an app. Below is my code:
AndroidManifest.xml
<activity android:name=".StartupActivity"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
StartupActivity.java
public class StartupActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent;
if (RandomClass.getSomeStaticBoolean())
{
intent = new Intent(this, ActivityOften.class);
}
else
{
intent = new Intent(this, ActivityRare.class);
}
startActivity(intent);
finish();
}
}
Both ActivityOften and ActivityRare are declared in the manifest (without the launcher category of course) and extend ListActivity and Activity respectively. 99% of the time the 1st activity to get shown is ActivityOften based on RandomClass.getSomeStaticBoolean().
So launching my app from the icon for the 1st time I break inside the StartupActivity.onCreate. The choice is properly made. But then any subsequent attempts to launch the app (from a shortcut or the apps menu) show the ActivityOften again. No further breaks occur inside the StartupActivity class. Despite the fact that I know that RandomClass.getSomeStaticBoolean() has changed value and that ActivityRare should appear, the 1st activity keeps popping up.
Any ideas?
Thanks, Merci, Gracias, Danke, Grazie!
Sean
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为您的应用程序活动是从历史堆栈加载的。
在
ActivityOften
和ActivityRare
的清单中设置android:noHistory=true
。那应该可以解决你的问题。It is happening because your application activity is loaded from the history stack.
Set
android:noHistory=true
in the manifest for bothActivityOften
andActivityRare
. That should solve your problem.作为建议,您可以通过动态选择内容视图来只进行一项活动,而不是三个活动。即
这意味着您必须在活动中的两个视图中编写设置代码,这可能会有点混乱。
Just as a suggestion, you could just have one activity instead of three by choosing the content View dynamically. i.e.
This would mean that you would have to write setup code both views in on activity, which can get a bit messy.