How to add flags with my intent in the manifest file - android

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 ?

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)

Related

When I charge my app after using home button it starts in my first activity

I'm sure that this will be a really newbie question but I'm stuck and I don't know how to get out of this!
I have an app that have three activities and when I use HOME button and I open the app again, it goes to the FIRST activity always, even if I was in the second or at the third one.
EDIT 3: My app comes in three activities, the first one is the main menu, the second is a map of tables and the third one are the data of the tables. Depending of the configuration, closing the third activity must bring me to the first one or the second one, and when I'm leaving the third Activity I dont want it to stay on the Activities stack. My program is working fine going from an Activity to another one. My problem is that seems that when I use Home Button my app finishes every Activity except the first one.
Maybe I have to modify anything on the manifest or maybe I have to use in a specifically way the RestoreInstanceState but I'm searching so hard and I can't find anything. Thanks in advance!
Edit 1: I'm adding my 'application' xml part of the Manifest:
<application
android:allowBackup="true"
android:icon="#drawable/icobaccus"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.example.tpv2_tablet.Activity_Start"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|keyboard"
android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.tpv2_tablet.Activity_Zonas"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|keyboard"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<activity
android:name="com.example.tpv2_tablet.PrintDialogActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|keyboard"
android:theme="#android:style/Theme.NoTitleBar">
</activity>
<activity
android:name="com.example.tpv2_tablet.Activity_Mesas"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|keyboard">
</activity>
</application>
Edit 2: Maybe I'm doing something wrong when calling other activities or I'm calling a wrong flag:
Intent intent = new Intent(Activity_1.this, Activity_2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
Remove
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
from all activities except launcher activity
To understand the reason why, read Google Documentation here
remove:
android:noHistory="true"
As this will remove the activity from the activity stack
remove:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
from:
Intent intent = new Intent(Activity_1.this, Activity_2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
to understand the reason check this answer
You need to set android:clearTaskOnlaunch="false" in your android manifest.

How to add flags to an APPWIDGET_CONFIGURE intent?

I have an activity registered on APPWIDGET_CONFIGURE:
<activity android:name="com.tahanot.activities.NearbyStops">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
However this activity does not behave as expected. It opens inside of an existing stack, and when I press the Back button, it takes me to other activities instead of closing the task. Ideally, I would like the APPWIDGET_CONFIGURE intent to include FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_MULTIPLE_TASK.
Is it possible to specify flags in AndroidManifest.xml, and if not, what workaround would you suggest?
Consider specifying launchMode attribute to the activity element.
<activity android:launchMode="singleTask" android:name="com.tahanot.activities.NearbyStops">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
As per the official docs,
FLAG_ACTIVITY_NEW_TASK
Start the activity in a new task. If a task is already running for
the activity you are now starting, that task is brought to the
foreground with its last state restored and the activity receives the
new intent in onNewIntent().
This produces the same behavior as the "singleTask" launchMode value,
discussed in the previous section.
Since you mention you want FLAG_ACTIVITY_NEW_TASK behavior so singleTask launchMode might work for you.
USe this java code when you start your activity
Intent intent = new Intent(this,
activityname.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Here you can add flag with your intent object like this.
And you can also add multiple flag.
This is the manifest declaration that I used, following appsroxcom's idea for android:launchMode:
<activity
android:name="com.tahanot.activities.NearbyStops"
android:configChanges="orientation"
android:label="#string/title_activity_stops_nearby"
android:launchMode="singleTop"
android:taskAffinity="com.tahanot.tasks.widgetConfiguration" >
<!-- android:launchMode makes sure the Back button doesn't navigate to another NearbyStops activity.
android:taskAffinity makes sure the Back button doesn't navigate to some other activity. -->
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>

How to export an activity so other apps can call it?

Well I searched a lot, but I didn't find a precise answer how to export an Activity, so an app can start it with startActivityforResult.
How do I achieve that? Do I have to change the Manifest in some ways?
As an alternate to Dalmas' answer, you can actually export an Activity without creating an <intent-filter> (along with the hassle of coming up with a custom action).
In the Manifest edit your Activity tag like so:
<activity
android:name=".SomeActivity"
....
android:exported="true" />
The important part is android:exported="true", this export tag determines "whether or not the activity can be launched by components of other applications". If your <activity> contains an <intent-filter> then this tag is set to true automatically, if it does not then it is set to false by default.
Then to launch the Activity do this:
Intent i = new Intent();
i.setComponent(new ComponentName("package name", "fully-qualified name of activity"));
startActivity(i);
Of course with this method you will need to know the exact name of the Activity you are trying to launch.
You need to declare an intent-filter in your Manifest (I took the following example from Barcode Scanner) :
<activity android:name="...">
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Then create an intent with the same action string :
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, code);
Android should start your activity (or it will show a drop-down box if there are multiple apps sharing the same action string).

How do I load an Activity from within a Library?

I've got a projected marked as "Is Library" - it has all the activities. I've got a new Android project that references this library. How do I mark one of my library's activities as the first one that starts up?
EDIT
My original manifest looks like this:
<activity android:name=".Welcome" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
All my activities are in the package com.rjs.animator.
As long as you AndroidManifest.xml contain those Activity tag and reference to the correct namespace/class within your Library, you should be good to go.
You have to add all activities and other components to the new Android project's manifest, with whatever <intent-filter> elements you want. Just put the standard MAIN/LAUNCHER <intent-filter> on whichever activity you want.

ActivityNotFoundException?

I am getting an ActivityNotFoundException in the following code:
Main.java
Intent intent = new Intent();
intent.setAction("com.test.app.TEST");
startActivity(intent); // ActivityNotFoundException
Manifest.xml
<activity android:name=".MainActivity" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.test.app.TEST" />
</intent-filter>
</activity>
I've had this issue too, as perfectly concisely described by jpahn.
the period at the front did not give any help to me.
even with exactly this (a copy of the original question including edits), I would still get ActivityNotFoundException.
Main.java
Intent intent = new Intent();
intent.setAction("com.test.app.TEST");
startActivity(intent); // ActivityNotFoundException
Manifest.xml
<activity android:name=".MainActivity" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.test.app.TEST" />
</intent-filter>
</activity>
This was resolved, after much trial-and-error, by simply adding this to the intent-filter in the manifest:
<category android:name="android.intent.category.DEFAULT" />
So the final manifest file contained:
<activity android:name=".MainActivity" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.test.app.TEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I got this error after moving an activity class from one package to another.
Clean build solved it (Project -> Clean).
Be sure to declare your activity in the manifest.xml within the aplication:
<application>
<activity android:name=".YourNewActivity"/>
</application>
To start the new Activity:
Intent intent = new Intent(main.this, YourNewActivity.class);
startActivity(intent);
Where main stands for the current activity,
Add a . (dot) before your activity name in Android Manifest. So it should be android:name=".WordsToSpeakMainActivity"
I have some addition to the #Tom Pace answer. The answer is completely right, but to make it more clear:
ActivityNotFoundException occurs because of absence of
<category android:name="android.intent.category.DEFAULT" />
Because when Android OS see this in the manifest file, understands that this activity can receive intent.
The point ActivityNotFoundException thrown is that, when activity(intent-creator-activity) tries to create intent for other activity(intent-receiver-activity), Android OS sees there is intent for receiver activity but receiver activity does not receive anyone. Then Android OS returns null or empty intent to intent-creator-activity. And startActivity throws that exception.
I have found a code from android developers to avoid this exception:
// Verify the original intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
Android Developers: Intent Filters
To be safe you can also call your new activity like this:
Intent intent = new Intent();
intent.setClass(this, THECLASSNAME);
startActivity(intent); //
However, you must add the activity to the androidmanifest - and write a . in front of it, e.g.
<activity android:name=".YOURACTIVITYNAME"></activity>
There two types of intents in android framework,
1-Implicit intents that you are using,
<activity android:name=".MainActivity" android:theme="#android:style/Theme.Dialog">
<intent-filter>
<action android:name="com.test.app.TEST" />
</intent-filter>
</activity>
just add one line in intent filter
<intent-filter>
<action android:name="com.test.app.TEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
2- Explicit Intents
Intent i=new Intent(CurrentActivity.this,WhereWeWantToGoActivity.class);
startActivity(i);
To launch an activity by a string definition, use:
Intent intent = new Intent();
intent.setComponent(
new ComponentName("com.app", "com.app.activity.TheActivity"));
startActivity(intent);
At the very top of your AndroidManifest.xml, you'll see the package attribute
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.example"
and then, in the activity tag, you'll see the name attribute:
<activity
android:name=".Something"
Make sure that the package name and activity name, when joined together, make the full package specification of your Activity i.e.
com.android.example + .Something = com.android.example.Something
Otherwise, you'll get a ActivityNotFoundException.
I found a solution to this problem... I´m using 2 modules in a android studio project, the thing here is that I needed to add the activity to the main manifest file
<activity android:name="com.HeadApp.ARTry.UnityPlayerActivity"
android:clearTaskOnLaunch="false" android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
/>
I had that in the unity activity manifest, I just copied the activity and paste it in the main manifest and that was it, hope it helps, eve been struggling a lot with this for the past 3 weeks

Categories

Resources