I have very strange situation.
Having one app, I decided to create another one from the code of first one.
I copied .xml files, copied .java files so that everything is OK.
But there's one HUGE problem: my onNewIntent(Intent intent)
method is called in first project, but it's not called in the second project (the code is the same!)
Method, which could trigger then, but can't trigger now
public void onClick(View arg0) {
Intent browserInt = new Intent (Intent.ACTION_VIEW,
Uri.parse("https://oauth.yandex.ru/authorize?response_type=token&client_id=zzzzz"));
browserInt.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(browserInt);
}
Here's onNewIntent() method:
@Override
protected void onNewIntent(Intent intent){
System.out.println(" I WORKED!");
Uri uri = intent.getData();
if (uri!=null) {
String m = uri.toString().split("#")[1];
String[] args = m.split("&");
String arg = args[0];
String token = arg.split("=")[1];
System.out.println(token);
}
}
I don't see "I WORKED" in my logs, unfortunately.
I've read lots of similar questions both on SO and over the Internet, tried setting Intent flags SINGLE_TOP, SINGLE_TASK and so on.
Here's the Android Manifest of WORKING project:
<application
android:name="yyy"
android:icon="@drawable/yaru_icon"
android:allowBackup="false"
android:label="xxx"
android:theme="@style/LightTheme">
<activity
android:name=".Main"
android:label="xxx"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
I'm quite desperate, why the similar code is not working anymore?
EDIT: I've tried everything: SINGLE_TOP, SINGLE_INSTANCE, SINGLE_TASK..
but then I occasionally did this on another activity:
Main m = new Main();
m.onNewIntent(this.getIntent());
And it finally worked!
I don't know, whether it's a dirty workaround or a bug, if anyone can explain it, please, comment.
The Activity you want to receive onNewIntent() in should have
Or add the flag tn intent
As documented in onNewIntent(Intent)
PREAMBLE:
Allright, I'm a little late to this one, but as I stumbled over the same issue and no answer here or for any of the other four stackoverflow questions, I found for this issue, solved the problem for me, here's what I figured out.
ANSWER:
There are several possible reasons, why onNewIntent isn't called and I'm gonna list them all - well all of which I know.
android:launchMode="singleTop"
manifest entry for the activity, where you want onNewIntent to be called, or the Intent used for starting the activity must have the flagFLAG_ACTIVITY_SINGLE_TOP
. You don't need both (it's a|
aka. logicalor
not a&
aka. logicaland
)! onNewIntent should also be called forandroid:launchMode="singleTask"
, but before you use that, you better check out the android:launchMode documentation, because it has much more consequences, than just one function call.android:launchMode="singleTop"
from working as specified and thus onNewIntent from being called. The bug was never officially solved, but I couldn't reproduce it in version 4.4.2 (Samsung S4 Mini). So it seems to have been fixed at some point between 4.0.x and 4.4.2.Not every time the preconditions as mentioned before are fulfilled, onNewIntent will be called. As the documentation of the function states:
That means, if the activity is newly created, onNewIntent won't be called, no matter what launchMode or Intent flags you did set!
As a variation of the solution described in the above blog post, you could also take advantage of the fact, that no matter if onNewIntent or onCreate was called, onResume will always be called afterwards, and do something like this:
For this example getIntent will always get you the Intent you used for the startActivity call or the Notification, as the new Intent will also be set for the Activity, if the Activity is freshly created (and thus onCreate was called).
POSTAMBLE:
Sorry for the long post. I hope you found something useful in it.
Here's one situation that might bite you, along with a bit more information:
onNewIntent
is called as expected with this code:but not with this code
The reason for that is that, in the second code block, the new activity is added on top before calling finish for the current activity, so to get the second code block to work you must add the
FLAG_ACTIVITY_CLEAR_TOP
flag as suggested by some of the other answers, like this:Note that
finish
is not called at all here, because it is called for you. From the documentation for the FLAG_ACTIVITY_CLEAR_TOP flag:I was using
onNewIntent
for search implementation in Android. I came across the problem thatonNewIntent
wasn't being called when I used the Go button on the keyboard in the emulator. I solved the issue by placing my code for handling the intent in theonCreate
method also. This needs to be done to make sure that the intent action is handled when a fresh instance of the activity is started.This posed a problem as
onCreate
is called whenever the activity is restored from a previous state too. So, the intent handling method gets called even when an orientation change occurs.Solution : Use
if (savedInstanceState==null)
to determine if activity is being restored from a previous state, or is it a fresh search.To enter to the onNewIntent method, you need in your AndroidManifest.xml file, put after set you main activity a launch mode, in this launch mode you have to put singleInstance e.g:
Now you will able to enter to you onNewIntent method.
The best way to handle
onNewIntent
withsingleTop
is simply this:Then do all of your logic on
getIntent
insideonResume
.