How to add flags with my intent in the manifest fi

2019-04-05 12:48发布

we know that there are flags which we can add to our intent using the addFlags() method in our java code. Is there any way we can add these flags in the manifest file itself instead of writing this in java code. I need to add REORDER_TO_FRONT flag for one of my activities in the manifest.

How to achieve this ?

4条回答
一夜七次
2楼-- · 2019-04-05 13:01

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.

查看更多
孤傲高冷的网名
3楼-- · 2019-04-05 13:08

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);
查看更多
别忘想泡老子
4楼-- · 2019-04-05 13:21

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>
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-04-05 13:26

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)

查看更多
登录 后发表回答