Android - resuming activities without killing previous activity - android

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.

Related

android Prevent a background app from launching activities in the foreground

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

How to put in foreground an Android activity that is in background

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().

How to come back to First activity without its onCreate() called

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.

Finishing multiple activities to resume an old one in stack?

I need to finish multiple activities in stack and resume an old one. For example,
There are four activities in stack Act1, Act2, Act3 and Act4. On some event in Act4, I need to finish Act4, Act3 and Act2 to resume Act1.
I tried using Intet.FLAG_ACTIVITY_CLEAR_TOP but it's recreating the activity which is not my requirement.
Advance thanks for suggestions.
You have used Clear_top, it will clear all the activities and bring act1 to front, but, onNewIntent method will be called of you activity, you can perform any task you want in that, after activity starts again.
Or, you can use flag FLAG_ACTIVITY_REORDER_TO_FRONT with your intent, it will do:
FLAG_ACTIVITY_REORDER_TO_FRONT do this:If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.
FLAG_ACTIVITY_CLEAR_TOP will do: 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.
for more information on flags look
http://developer.android.com/reference/android/content/Intent.html
There are four activities in stack Act1, Act2, Act3 and Act4. On some
event in Act4, I need to finish Act4, Act3 and Act2 to resume Act1.
Do this:
Intent intent = new Intent(Act4.this, Act1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
use android:launchMode,will be ok.
<Act1
...
android:launchMode="singleTask">

Restarting Android application after process is killed

When my application is idle, Android kills the process.
If user reopens the application after some time, only the top Activity is created - this is a problem for me because the activity depends on initialization of other objects (which are now destroyed).
What I want to do in that case is to re-launch the application.
How can I do that?
Just identify that your Application is being launched after it was previously destroyed by Android, you could do this by keeping a variable in a custom Application class, and set it to true after your applicaiton is initialized. So when the applicaction is re-launched, this flag is false, and then just make an Intent to launch your main Activity specifying FLAG_ACTIVITY_CLEAR_TOP :
Intent reLaunchMain=new Intent(this,MainActivity.class);
reLaunchMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(reLaunchMain);
I think this answer only for you.
After finish progress call this
finish();
Intent intent = new Intent(this, sameactivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
You should probably be looking at storing such Objects in your app's implementation of the Application class.
If these objects contain state that needs to be more persistent, you should save the state of such Objects in each Activity's onPause() method, either to the database, in SharedPreferences or remotely.

Categories

Resources