My MainActivity has the lauchMode="singleTask"
Now I want to start the activity from a notification with special intent data.
in MainActivity.onResume I access the given intent data...
The problem is: When the activity already exists, and I click on the notification, the activity comes to foreground, but the method onResume is not called and I cannot access the intent data.
I tried the flag FLAG_ACTIVITY_CLEAR_TASK and this works for Honeycomb but not for Gingerbread.
This is how I start the activity from a notification:
Intent intent = new Intent();
intent.setClass(this, MainActivity.class);
intent.putExtra("triggerid", triggerid);
startActivity(intent);
onResume() is always called if the activity is not in the foreground. However, what you're probably seeing is that getIntent() is returning the intent that started the activity, not the intent that was most recently sent to it.
To fix this, you should override onNewIntent(). This will receive the new intent sent to it. Then you can call setIntent() with the received intent, which will cause getIntent() to return the new intent when used in onResume().
Related
Form Activity Login I call
Intent intent=new Intent(LogIn.this,BRInfoActivity.class);
From Activity BrActivityList I Call
Intent intent=new Intent(BRActiviList.this, BRInfoActivity.class);
intent.putExtra("BRinfo", objBR);
And Now in BRInfoActivityList
Intent i = getIntent();
BrInfoEN objBR=(BrInfoEN)i.getSerializableExtra("BRinfo");
Now i want if(intent==BrActivityList)The last two above line execute otherwise does not.How to do it?
If you will start activity with
startActivityForResult().
Then you can use getCallingActivity() to get caller.
Form Activity Login call this--
Intent intent=new Intent(LogIn.this,BRInfoActivity.class);
intent.putExtra("call_from","ActivityClass");
From Activity BrActivityList Call--
Intent intent=new Intent(BRActiviList.this, BRInfoActivity.class);
intent.putExtra("call_from", "BRActivity");
Now, when you get the intent, compare the "call_from" string in an if-else block! That's it!
May this help you...
The new intent comes as part of onNewIntent(Intent). The original Intent is still available via getIntent().
You put whatever code you need to into onNewIntent in order to update the UI with the new parameters; probably similar to what you're doing in onCreate.
Also, you probably want to call setIntent(intent) in onNewIntent to make sure future calls to getIntent() within the Activity lifecycle get the most recent Intent data.
I have a stack of activities, and use the following code to bring the main activity to the 'active' state:
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.putExtra("clearCache", true);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
The problem is that when I try to retrieve the clearCache extra, a call to getIntent().getExtras() returns null.
My understanding is that because the activity that I'm launching was already on the stack, and because I set the Intent.FLAG_ACTIVITY_CLEAR_TOP flag, the Intent that gets returned will be the original intent.
How do I access the calling intent in the activity I'm launching?
In the google documentation on FLAG_ACTIVITY_CLEAR_TOP, you should be getting the new intent each time:
"If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent."
...
"The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent()."
For more details take a look here:
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
Hope that helps!
You just change your code by passing the particular Activity name and keep the rest of code as it is,
Intent i = new Intent(MapActivity.this, MainActivity.class);
i.putExtra("clearCache", true);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
I have a singleTop activity, this activity is launched via Intent:
Intent intent = new Intent(GCMIntentService.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
onNewIntent is called.
My problem is that then I hit home button, and then launch application again (from background) onNewIntent is called again with the same intent, and I can not find a way to discard this Intent.
I tried to putExtra flag to this intent, but it is not persisted between application state changes.
Is it possible to stop calling onNewIntent() when application comes to foreground?
In your manifest add the line below to your MainActivity
android:finishOnTaskLaunch="true"
Inside a broadcast receiver I want to start my app (Activity) and pass in some data.
My problem is that the extras don't seem to carry over into the activity. I am trying to get the data inside the onNewIntent(Intent i) function.
Any ideas?
Here is my current attempt in the BroadcastReceiver:
Intent intSlider = new Intent();
intSlider.setClass(UAirship.shared().getApplicationContext(), SliderMenuActivity.class);
intSlider.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intSlider.putExtra("action", ScreensEnum.Object);
intSlider.putExtra("objectId", objectId);
intSlider.putExtra("objectCode", objectCode);
intSlider.putExtra("userId", userId);
UAirship.shared().getApplicationContext().startActivity(intSlider);
EDIT - Added code used in onNewIntent() and onCreate()
The following code works great in onCreate() when the app isn't currently running. For when the app is already running the same code doesn't work (i.e. no extras) from the onNewIntent() function.
Intent intent = getIntent();
if(intent.hasExtra("objectId")) {
loadDetail(intent.getStringExtra("objectId"), "2w232");
}
The problem is getIntent() method. It always returns the intent that started the activity, not the most recent one. You should use intent that was passed to onNewIntent method as an argument.
We stumbled upon this problem once, when we were trying to launch/call onNewIntent on an Activity in response to a local notification tap. The extras that we put on our Intent were disappearing at the time onNewIntent received it.
I don't remember this being documented anywhere back then, but the "problem" was that we weren't setting the action field on the Intents that we prepared. Turns out if the Intent received by your Activity doesn't have an action set using setAction, the system still delivers the Intent to its destination, but doesn't transmit the extras you have set while creating the Intent.
TL;DR:
If you encounter this problem with an Intent with no action, calling setAction to set an arbitrary action value before sending the Intent might fix it.
Extract from the docs
This is called for activities that set launchMode to "singleTop" in
their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag
when calling startActivity(Intent). In either case, when the activity
is re-launched while at the top of the activity stack instead of a new
instance of the activity being started, onNewIntent() will be called
on the existing instance with the Intent that was used to re-launch
it.
An activity will always be paused before receiving a new intent, so
you can count on onResume() being called after this method.
Note that getIntent() still returns the original Intent. You can use
setIntent(Intent) to update it to this new Intent.
I think the last paragraph explains your problem.
You have to set the flag FLAG_ACTIVITY_SINGLE_TOP or set launchMode singleTop in the manifest file.
Of course when onNewIntent is called you do not use getIntent but the Intent received as argument.
onNewIntent will be called when the activity instance already exists. For example, if last time you pressed the Home Screen button.
I wrote a solution that worked for me here: Intent with old extra in onCreate() for singleTask Activity
You can store the last received intent in a member variable (mLastIntent).
Then you can use this member in your onResume() method to query for your extra data.
private Intent mLastIntent;
#Override
protected void onNewIntent(Intent intent) {
mLastIntent = intent;
};
I learned that the Android launcher resets the homescreen to the default page when onNewIntent() is called. I know that onNewIntent() is called when the activity receives another start intent while it's already in the foreground. Is there a way to call the onNewIntent() of the activity when it's in the background?
I have tried several flags like FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_REORDER_TO_FRONT but none seems to call onNewIntent.
Any help would be appreciated
Try this, It will Help You,
Intent intent = new Intent(MainActivity.this, TargetActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.startActivity(intent);