How to add flags with my intent in the manifest file
Asked Answered
G

4

18

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 ?

Gorcock answered 11/7, 2011 at 13:5 Comment(0)
M
12

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);
Misfortune answered 14/7, 2011 at 11:12 Comment(1)
Hi Richie, yeah I know this thing, but the problem is I am not explicitly launching this activity from another activity. In my case the Activity to be launched is the main/launcher and it's intended to start when the user closes other activities by hitting the back button and finally comes to this screen (which is also the starting screen)Gorcock
F
7

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.

Frore answered 12/8, 2015 at 5:24 Comment(1)
Excellent hint, thank you! Instead of just "playing around" one might also want to check the documentation of the <activity> element: developer.android.com/guide/topics/manifest/…Estonian
U
6

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>
Ulla answered 3/10, 2013 at 22:38 Comment(0)
E
0

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)

Echelon answered 26/9, 2017 at 23:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.