【问题标题】:How to add flags with my intent in the manifest file如何在清单文件中根据我的意图添加标志
【发布时间】:2011-07-11 13:05:11
【问题描述】:

我们知道,我们可以使用 java 代码中的 addFlags() 方法将标志添加到我们的意图中。有什么方法可以在清单文件本身中添加这些标志,而不是用 java 代码编写。 我需要在清单中为我的一项活动添加 REORDER_TO_FRONT 标志。

如何做到这一点?

【问题讨论】:

    标签: android android-intent android-manifest flags


    【解决方案1】:

    在清单文件中,您不能添加 Intent 标志。您需要在 Intent 中设置您传递给 startActivity 的标志。这是一个示例:

    Intent intent = new Intent(this, ActivityNameToLaunch.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intent);
    

    【讨论】:

    • 嗨 Richie,是的,我知道这件事,但问题是我没有明确地从另一个活动启动这个活动。在我的情况下,要启动的 Activity 是主/启动器,它打算在用户通过点击后退按钮关闭其他 Activity 并最终进入此屏幕(这也是起始屏幕)时启动
    【解决方案2】:

    我遇到了类似的问题,想设置标志

    Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK
    

    为了使活动始终处于顶部。

    在这种情况下,解决方法是设置属性

    android:launchMode="singleInstance"
    

    在清单中。

    一般来说,一个 Activity 的 Android 清单中有许多属性,您可以尝试使用这些属性来获得与使用标志类似的效果。

    【讨论】:

    【解决方案3】:

    要回答原始问题,因为这在 google 搜索中显示为第一个答案,所以可以完成,因为 API 级别 3(2009 年引入),如所述,将 android:noHistory="true" 添加到清单文件中的活动定义中这里:http://developer.android.com/guide/topics/manifest/activity-element.html#nohist

    示例:

    <activity
       android:name=".MainActivity"
       android:label="@string/app_name"
       android:noHistory="true">
      <intent-filter>
          <action android:name="android.intent.action.MAIN"/>
          <category android:name="android.intent.cataegory.LAUNCHER"/>
      </intent-filter>
    </activity>
    

    【讨论】:

      【解决方案4】:

      您可以通过在清单的&lt;activity&gt; 节点中使用android:launchMode="singleTop" 轻松实现此目的,如下所示:

      <activity
          android:name=".ui.activities.MainActivity"
          android:launchMode="singleTop">
              <intent-filter>
                  <action android:name="android.intent.action.MAIN" />
                  <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
      </activity>
      

      请注意,@jörg-eisfeld 给出的android:launchMode="singleInstance" 不是一般用途的推荐选项,正如官方文档中所述:https://developer.android.com/guide/topics/manifest/activity-element.html(参见 android:launchMode部分)

      【讨论】:

        猜你喜欢
        • 2015-03-18
        • 1970-01-01
        • 1970-01-01
        • 2011-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多