Description:
Activity A is visible (or in the background)
Intent I is received by a broadcast with valuable extras and then passes the extras to a new Intent I2 that will be used to start activity A.
Outcome: Do not bring activity to front if activity is in background.
Code:
Intent I2= new Intent(context, MyActivity.class);
I2.putExtra(..
I2.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); // | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(I2);
Note: I added no "android:taskAffinity" to manifest.. i thought you should know
if you want your activity to be in background add this line in the oncreate of activity
moveTaskToBack(true);
You can use this line in your onCreate() method:
moveTaskToBack(true);
You don't want to start an Activity in the background. There are better ways to do what you want. You can have your Activity register to receive the broadcast Intent, for example. It will get the call to onReceive() even if it is in the background. You can determine if your Activity is in the background by setting a variable to true in onPause() and to false in onResume(). Then in onReceive(), if the variable is true you are in the background.
Related
I tried to set intent.setFlags (FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK), but the effect is not as I think.
It will execute onResume () in the old A activity.
But I want the effect as shown below:
A activity onCreate() ... onPause()
anywhere(from notification, from B activity ...) start A activity again
old A activity onDestory()
create new A activity
you should use FLAG_ACTIVITY_CLEAR_TOP
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.
SAMPLE CODE
Intent i = new Intent(YourActivity.this, OTHERACTIVITY.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
use (FLAG_ACTIVITY_CLEAR_TOP) flag when u passing intent. then after startactivity() use finish() method.
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 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"
What method should I use to move my app to background and then move it to foreground again?
I tried using moveTaskToBack(true) and the activity is moved to background successfully but then I can't move it to foreground.
I tried starting the activity again using startActivity() but with no success and there seems to be no method moveTaskToFront() or something similar.
Use moveTaskToBack() to move your app to the background.
To move it to the foreground, use the following code:
Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
startActivity(intent);
If you are trying to do this from a Service or BroadcastReceiver then you will need to do this before calling startActivity():
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Thanks it worked for me by adding following intent
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);