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);
Related
I have 3 activity . Activity A ,Activity B, Activity C.
This is the flow A->B->C.
Now i want to come to activity A from C .(i.e C->A) without getting its onCreate() called of Activity A.
so far my code is:But it will call onCreate() of ActivityA.
I want to call Restart().
Intent intent=new Intent(ActivityC.this,ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Two cases:
If you want to keep other activities live and just want to bring A to front, just use intent flag FLAG_ACTIVITY_REORDER_TO_FRONT.
If you don't want to clear all activities and want existing instance of A at top, then use intent flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP.
Note : If you use only FLAG_ACTIVITY_CLEAR_TOP, then onCreate will be called.
Keep android:launchMode="singleTask" in manifest at activity declaretion
An Activity with singleTask launchMode is allowed to have only one instance in the system
If instance already exists, i will not call 'onCreate' it will call 'onNewIntent' method.
See http://androidsrc.net/android-activity-launch-mode-example/ for better understand about launch modes.
Although setting the launch mode to singleTask will work, use of that launch mode is discouraged. The documentation for launch mode indicates singleTask is "not recommended for general use".
The desired behavior can be achieved using Intent flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP:
Intent intent=new Intent(ActivityC.this,ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();
Activity A will not be recreated and will receive the intent via onNewIntent().
Use SingleTask launch mode for Activity A.
New intent will be delivered to existing instance.
For an App I am developing, I want to re-launch the current activity using an intent. So I'm in MainActivity.class and I want to re-launch MainActivity.class using the following:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
This calls onDestroy() but does not re-launch the activity. Why doesn't this work?
If your are in an Activity: this.recreate();
If your are in a Fragment: getActivity.recreate();
Related Links :
How do I restart an Android Activity
how do I restart an activity in android?
Android activity restart
You could just use:
finish();
startActivity(getIntent());
Which will finish the current activity, and start a new activity with the same intent that you received when the activity was originally created. This should effectively re-launch the activity as is.
Edit:
See Reload activity in Android
Just do this:
Intent i=getIntent();//This simply returns the intent in which the current Activity is started
finish();//This would simply stop the current Activity.
startActivity(i);//This would start a new Activity.
Include this one ...
startActivity(intent);
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"
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().