I have an activity in my application that does a process and when finished, launches an activity with a normal intent:
Intent intent2 = new Intent();
intent2.setClass(anActivity.this, JustAnotherActivity.class);
startActivity(intent2);
finish();
The problem is that when you are doing the process, if I press the home button and take it to the background, when it finishes and calls the new activity, even if it is in the home of the phone, this new activity jumps to the foreground.
I am looking for the activity to be launched, but if the user is outside the application, it does not come to the foreground.
I guess the use of a FLAG is necessary, but the ones I have tried have not worked.
sorry for my bad english
Thanks.
what if you take care of ActivityLifecycle (OnPause, OnStop).
You can add a flag when the activity goes "OnPause/OnStop" depending what you want. And them perform the intent just if the activity is not "Pause/Stop".
Activity Lifecycle
Related
I want to know how to switch between 2 activities without killing any of the 2 activities, So that I can resume it's state.
I tried this:
Activity1.class
Intent i=new Intent(this,Activity2.class)
startActivity(i);
Activity2.class
Intent i=new Intent(this,Activity2.class)
startActivity(i);
This codes just creates another activity and destroys the previous activity when I press back button. What I need is how can I switch activities while pausing the previous and resume to the previously paused activity without killing the activity I'm at.
You can use FLAG_ACTIVITY_REORDER_TO_FRONT, and if you already have an instance of the activity it will bring it to front and call its onResume().
The if is because Android can kill your background activity any time if the system lack of resources.
Intent i=new Intent(this,Activity2.class)
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
Also check FLAG_ACTIVITY_PREVIOUS_IS_TOP, depends on your app logic.
You can try to set flag on Intent (Intent.FLAG_ACTIVITY_NEW_TASK).
But you have to remember that system may kill your activity when he needs memory.
I read several similar questions here, but I didn't find a clear reply to my question.
I launch my Android App and I have my main ActivityA in foreground
after some time I push a button (of ActivityA) and I open (and put in foreground, then visible and ontop) ActivityB. I do it simply by the command myContext.startActivity(myIntent);
It means that now ActivityA is in background (onPause()), then not visible.
After some time I push another button of ActivityB with the target to put in foreground (then visible and ontop) again previous ActivityA
What is the correct and best way to do it? According to my understanding (but I'm not sure it's correct.) it shouldn't be by startActivity(), because startActivity() creates another instance of ActivityA (it calls onCreate() ) and then there will be 2 instances of ActivityA running (one in foreground and one in background). What I want to get is a calling of onResume() for ActivityA (and not of onCreate() ).
The second question is: how can I know if ActivityA is still alive in background? Maybe after sometime the system killed it to free resources.
Note: the solution in my case cannot be to use finish() to destroy ActivityA when I open ActivityB, and then to use startActivity() to reopen it, because I need ActivityA alive as much as possible.
Thank you very much in Advance
Fausto
What you need is the FLAG_ACTIVITY_REORDER_TO_FRONT when starting a new activity. This will cause a background activity to be be brought to the foreground if it's running, or create a new instance if it's not running at all.
From inside ActivityB:
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
What is the correct and best way to do it?
Use startActivity(), with an Intent on which you have added Intent.FLAG_ACTIVITY_REORDER_TO_FRONT.
it shouldn't be by startActivity(), because startActivity() creates another instance of ActivityA
While that is the default behavior, Intent flags can alter that behavior.
how can I know if ActivityA is still alive in background?
If you did not finish() it, and your process has not been terminated, it exists.
Maybe after sometime the system killed it to free resources.
Android terminates processes to free up system RAM. It does not destroy activities on its own.
because I need Activity A alive as much as possible
To be honest, that suggests that you have other architectural issues. Bear in mind that activities are destroyed and recreated for various reasons, such as configuration changes (e.g., screen rotation). Activities should be very disposable.
You can use following
1. For launching new instance (current state of ActivityA) and get ActivityA on Top of Stack
Intent intent = new Intent(this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
2. For launching old instance and get ActivityA on Top of Stack
Intent intent = new Intent(this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
For more detail please check
Task and Back Task
You can use StartActivityForResult in the place of StartActivity in activity A and on activity B you can setResult when you want to open Activity A again.In that case OnActivityResult() of activity A is called not onCreate().
I want some directions here. I have 3-Activities in an app i.e. MainActivity, MapViewActivity and DbActivity. I goes from one activity to another via. intents. The code of each intent is:
Intent intent = new Intent(this, ActivityName.class);
startActivity(intent);
I have used
android:noHistory="true"
in all these activities, so that when the mobile "end" button is pressed the app gets stop. Otherwise app was going back..... in spite of stoping the app
Now the problem is: When app goes for the GPS settings after finding GPS service off, the user turns on GPS and presses the mobile "end" button to come back to the app. But app also get stop. So user again start the app and comes on MapViewActivity. If I remove android:noHistory="true" from MapViewActivity, the problem also comes back. So is there any other solution for. Please guide me, if there is some method...
I don't think you mean "end". You probably mean "back". If you don't want the user to return to the activity when the user clicks "back", then just call finish() after you start the next activity. Like this:
Intent intent = new Intent(this, ActivityName.class);
startActivity(intent);
// finish this activity so that the user doesn't come back to it
finish();
Please take out the android:noHistory="true" from the manifest, this probably isn't what you want.
I have an application where after a certain amount of time I want all activities in the app's stack to be removed and replaced by a login activity screen. I've got a Runnable timer that issues this set of statements:
Intent intent = new Intent( ctx, mainActivity.class );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
ctx.startActivity( intent );
This works fine when my app is in the foreground, however, when my app is in the background and I'm using another app (say, the Web Browser), the timer fires as it should and pops up the login activity screen to the foreground.
What I want is for the login activity to be activated and moved to the front of the stack, and have all other activities in the stack removed, but not have the app and activity popped to the foreground over any currently running app.
Is there any way to do this with a different method or flag? Thanks.
You could probably finish() the login activity in the onCreate event, conditional on an intent extra. Hopefully it would then be closed before it is displayed...
i have a button to close my app with this code:
finish();
the problem is that this button doesn't exit of my app... it simply closes the current intent ant returns to the previous intent (window) of my app....
how i can do a real exit/close button?
i tryed with this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but it doesn't works, because when i turn back into my app, the app comes in the last opened window, and not in the first window of the app, how i can do that? i need that when i re-open my app it starts on the first window of my app
If you really want your app to die. You could initiate each intent with startActivityForResult(). then before each finish() set the result to send back. in each parent activity you can override onActivityResult() to test whether the result received means the application needs to end. if so you can call another set result and finish(). repeat this in all activities and you will find that your application terminates entirely.
Incidentally I'm writing this from memory. function names may not be exact.
Hope that helps.
p.s. re-read your requirements. you can always stop the finish loop at your first activity.
I would do it this way:
I would define my initial activity (i.e. MainMenu) with a Launch Mode of singleTop
I would then invoke my MainMenu from the activity that is going to close the application.
startActivity(new Intent(this, MainMenu.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("closeProgram", true);
Then override the onNewIntent in the MainMenu activity; check for the extra boolean of "closeProgram", if the value is true, then do a finish();
Haven't tried it but I think it should work.
I recommend you read this: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
Chances are, you don't want an exit button. Perhaps a logout button, but that's it.
finish() closes activity (this is what you call intent, but it's not correct), not application. Application can not be finished at all (only forcefully killed like task killers do). You should design your activity stack in such a way that it will suit your needs. May be you should look at stack rearrangement examples in ApiDemos.
you can try this: System.exit(0);