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"
Related
Neither
val intent = Intent(sourceActivity, SomeActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)
nor
val intent = Intent(sourceActivity, SomeActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
triggers onNewIntent as described by 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.
They always try to recreate SomeActivity, calling onCreate instead!
Expected behaviours:
SomeActivity, running but not on foreground, paused, is brought back to foreground
SomeActivity's back stack is preserved
SomeActivity receives some info - I want to inform SomeActivity of a success/ failure
SomeActivity instance is reused vs. a duplicated is created anew - so FLAG_ACTIVITY_SINGLE_TOP or android:launchMode="singleTop" is not derisable
Interestingly, the Manifest.xml approach works as expected!
<activity
android:name=".webview.SomeActivity"
android:launchMode="singleTask"
android:configChanges="screenSize|orientation" />
What am I missing?
This has worked for me :
intent = new Intent(this, HomeScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
If this still doesn't work and you want to clear all activities before starting a new one you can use finishAffinity() before starting a new activity.
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.
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);
I have an Activity that is defined to be singleTop so that only the one instance will exist.
<activity
android:name=".MyMainActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:launchMode="singleTop" android:multiprocess="true">
I set up a Notification Intent with some data, and include it in a PendingIntent, and send it to the Notification Manager.
Intent notificationIntent = new Intent(context, MyMainActivity.class);
notificationIntent.setAction(MyMainActivity.CustomInternalMessageAction);
notificationIntent.putExtra(MyMainActivity.ReceiveTestMessage, rawMessageText);
...
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
mNotificationManager.notify(number, notification);
If the actiivty is not running, the intent starts the activity via onCreate() as expected.
If The activity is running, but in the stopped state / not in foreground (like when clicking the home button), and the notification is clicked, my activity's onNewIntent() is called as expected.
However, if the Activity is already called when it's in the foreground, then onNewIntent() doesn't get called.
How can I set up this notificaiton so that my singleTop Activity gets sent the intent, when it's in the pause/stop state (and also still functions in the other cases I mentioned). I'm thinking there's a flag to set on my notificationIntent object, but the functions of the flags aren't really clear for the case of singleTop activities.
If your application is composed by more than one activity, the remain activities need to be defined as singleTop as well if you want to receive a onNewIntent()call when that activity is active.
Let suppose that main activity is A and is defined as singleTop and from there you start activity B not defined as singleTop. If now you select a notification that calls your application, the application will start on activity B but onNewIntent() is not called.
You can also override this behaviour adding the flag Intent.FLAG_ACTIVITY_CLEAR_TOP which remove activity B from stack and Activity A will recive the call on onNewIntent().
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.