How to add flags with my intent in the manifest file

女生的网名这么多〃 提交于 2019-12-04 00:03:48
Vineet Shukla

In manifest file you can not add Intent flags.You need to set the flag in Intent which u pass to startActivity. Here is a sample:

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

To answer the original question, since this appears as the first answer in the google search, it can be done, since API level 3 (introduced in 2009) with adding android:noHistory="true" to the activity definition in the manifest file as described here: http://developer.android.com/guide/topics/manifest/activity-element.html#nohist.

example:

<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>

I had a similar problem and wanted to set the flags

Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK

in order to bring the activity always to top.

In this scenario, the solution is to set the attribute

android:launchMode="singleInstance"

in the manifest.

Generally, there are many attributes in the Android manifest for an activity, and you may play around with these to get similar effects as with flags.

You can easily achieve this with using android:launchMode="singleTop" in the <activity> node of manifest, like this:

<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>

Note, that android:launchMode="singleInstance" as it is given by @jörg-eisfeld is not recommended option for general use, as it is stated in official documentation: https://developer.android.com/guide/topics/manifest/activity-element.html (see the android:launchMode section)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!