Android:动态选择启动活动并不总是有效

发布于 2024-12-05 21:41:05 字数 1375 浏览 0 评论 0原文

我在这里(和其他地方)阅读了一些文章,这些文章描述了如何在启动应用程序时动态选择要显示的活动。下面是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始终不够 2024-12-12 21:41:05

发生这种情况是因为您的应用程序活动是从历史堆栈加载的。
ActivityOftenActivityRare 的清单中设置 android:noHistory=true。那应该可以解决你的问题。

It is happening because your application activity is loaded from the history stack.
Set android:noHistory=true in the manifest for both ActivityOften and ActivityRare. That should solve your problem.

心清如水 2024-12-12 21:41:05

作为建议,您可以通过动态选择内容视图来只进行一项活动,而不是三个活动。即

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    if (RandomClass.getSomeStaticBoolean())
    {
       setContentView(R.layout.Often);

       // Set up often ....
    }
    else
    {
       setContentView(R.layout.Rare);

       // Set up rare ....
    }
}

这意味着您必须在活动中的两个视图中编写设置代码,这可能会有点混乱。

Just as a suggestion, you could just have one activity instead of three by choosing the content View dynamically. i.e.

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    if (RandomClass.getSomeStaticBoolean())
    {
       setContentView(R.layout.Often);

       // Set up often ....
    }
    else
    {
       setContentView(R.layout.Rare);

       // Set up rare ....
    }
}

This would mean that you would have to write setup code both views in on activity, which can get a bit messy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文