Intent to start activity - but don't bring to front
Asked Answered
D

3

38

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

Drank answered 4/4, 2012 at 9:59 Comment(3)
why do you want to startActivity when you don't want it to be in foreground,..? what is that you want to do..?Vlad
So I updated it: DO NOT BRING activity to front IF activity is in background!Drank
Don't use FLAG_ACTIVITY_MULTIPLE_TASK for this! If you end up starting multiple tasks you will have a very hard time managing them. This is a can of worms that you do not want to open!Zimmerman
T
15

if you want your activity to be in background add this line in the oncreate of activity

moveTaskToBack(true);
Trometer answered 4/4, 2012 at 10:4 Comment(4)
How to make it works for FLAG_ACTIVITY_MULTIPLE_TASK ? (2 tab button A and B, but should make it hide the app by any button's task instead of button B back to button A single direction) (FLAG_ACTIVITY_TASK_ON_HOME also single direction.)Mispickel
[UPDATE] I can use global reference for first activity and make it act.moveTaskToBack(true); .Mispickel
This should be an answer. @林果皞. I will try it now and see if ti is still valid.Yellowlegs
This leads showing white screen very short time and hide it, isn't there any solution to open the activity totally in background without showing anything at all?Haggerty
N
7

You can use this line in your onCreate() method:

moveTaskToBack(true);
Nettienetting answered 4/4, 2012 at 10:9 Comment(1)
can you tell me how can i restart activity after call moveTaskToBack(true); i used Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_ACTIVITY_MULTIPLE_TASK but not restart the activity its make new one .. note** i instance same activity many timesShowing
Z
5

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.

Zimmerman answered 13/6, 2016 at 18:41 Comment(3)
The question here is not how to check the current status of the activity, but how to prevent bringing back the application when it is in the background with the Intent still being sent.Grannie
@DamonYuan You can't do that. If you launch an Intent for an Activity, the task hosting that Activity will be brought fo the foreground. That's the way it works. You cannot get around that. What you can do is to use a different mechanism to get the behaviour you want (like BroadcastReceiver, which will NOT cause the hosting task to be brought to the foreground.Zimmerman
That's my understand. So your original answer is not the one to this question, but your comment above is the right one.Grannie

© 2022 - 2024 — McMap. All rights reserved.