How to add flags with my intent in the manifest fi

2019-04-05 12:28发布

问题:

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 ?

回答1:

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


回答2:

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>


回答3:

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.



回答4:

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)