【问题标题】:Don't declare main/launcher activity in manifest不要在清单中声明主/启动器活动
【发布时间】:2014-11-03 08:33:54
【问题描述】:

在我的应用程序中,我在首次启动时启动初始设置活动。

// main activity onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

if (PreferencesUtility.firstStartup(this) ) {// helper class to access SharedPreferences
    Intent setupActivity = new Intent(this, SetupActivity.class);
    startActivity(setupActivity);

    finish();
    return;
}
...
...

}

这段代码可以运行,但我认为更好的地方是在我的应用程序类中。这不需要在“初始启动”检查后立即开始和完成主要活动......

有没有办法动态选择启动活动? 我试图从 Manifest.xml 中删除“主”和“启动器”意图过滤器 并在我的 Applications onCreate 中添加了以下代码

    Intent startIntent = new Intent(this, LogBookActivity.class);

    startIntent.setAction(Intent.ACTION_MAIN);
    startIntent.addCategory(Intent.CATEGORY_LAUNCHER);  


    startActivity(startIntent);

但是什么都没有启动。 我认为至少我的应用程序的 onCreate 会被调用...

【问题讨论】:

  • 如果你没有在manifest中指定Main Activity,系统怎么知道启动哪个Activity?
  • 我同意 shyam,我不认为,这是可能的
  • 我想我可以把这段代码放在我的自定义应用程序类中的 onCreate 方法中,如上所述
  • android在哪里做这个?有人必须阅读 manifest.xml 并启动配置的活动......这不应该是 Application 类吗?
  • 没有。即使您在Application 类中将活动声明为MAIN,也已经为时已晚。因为 manifest.xml 在你的任何类之前被首先读取。当阅读完成后,接下来发生的事情是“我应该先启动哪个类?我先启动 MAIN 类,但我没有看到任何标记为 MAIN。”

标签: android android-intent android-manifest


【解决方案1】:

您真的需要为此启动一个单独的活动吗? 你为什么不为这些初始步骤创建一个片段,那么你不必终止你的主要活动,而只需切换到这个片段。

【讨论】:

  • 我为它创建了一个单独的 Activity,因为我还想从 PreferencesActivity 导航到它...但实际上是一个很好的输入
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多