I have a question regarding activity multiple instance.
eg)
A1 is an activity, A1 starts A2 using startActivity(), then A2 starts A1 using startActivity() as well, how many instance of A1 exists in current system?
AS you asked there will be only one instance of A1 will be present.
If you didnot finish A1 while going to A2 then new instance will be
created while coming back to A1.
If you didnot finish then the background running instance of A1(
which will be in stack) will come to the front.
here is example
Intent i=new Intent(A1.this,A2.class);
startActivity(i);
finish();
This is my point 1.In this case if you will come back from A2 then new instance will be created.
Here is my second point just without finish();
Intent i=new Intent(A1.this,A2.class);
startActivity(i);
In this case if u will come back from A2 then A1 is already being running in background because you have not finished it while going to A1.So it will come to front.
It depends on the flags you set on the intent when you start the activity. If for example you set them to Intent.FLAG_ACTIVITY_REORDER_TO_FRONT, then the activity will be reused, i.e. no multiple instances. If you don't set any flags, then the scenario you describe will launch a new activity on the stack. Eventually you'll run out of memory. Only calling finish() will destroy the activity instance
Related
I have an initial activity O and one more activity A in which i can select to go to activities A1,A2,A3 and for example fill in a form in each of them. So i follow this path:
O>A>A1>A>A2>A>A3
While i am at A3 i want to press the back button and go to O again but i will have to pass from every instance of A (let's assume i use finish() or no history in the manifest for A1,A2,A3 so they will be not present in the stack)
how can i declare that A will only have one instance (the last one) in the stack, so that if i press back button twice i will go to O again?
#Override
public void onBackPressed() {
Intent intent = new Intent(this,O.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
Change the launchMode of activity A to singleTop. As it states in the documentation:
If an instance of the activity already exists at the top of the target
task, the system routes the intent to that instance through a call to
its onNewIntent() method, rather than creating a new instance of the
activity.
Now, instead of going from A -> A1 -> A, you can just finish() activity A1 (or use the up button), which will return you to the instance of activity A.
I have two android activities(lets call them A1 and A2). The purpose of the first one ist to display several linear layout items. Also it features a button to add new items. When clicking on the add button using the following intent:
//...
Intent intent = new Intent(this, CreateActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
//...
the application should switch to A2 pausing the first one. When accessing a type on A2 I switch back to A1 with this:
//...
Intent intent = new Intent(super.getContext(), HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("type", this._type);
super.getContext().startActivity(intent);
//...
intent, adding a new item to the layout which works.
Now I've got the onCreate, onResume, onPause methods which are called every time I switch from A2 to A1. Is that the normal android behaviour?
As far as I understood the activity lifecycle the flags NEW_TASK, and CLEAR_TASK open up a new process pausing the Activity they're called in.
I've read several different documentations about switching Activities in android but didn't really get the point of how to explicitely tell an Activity to just pause and not call onCreate etc. again.
I think here your Activity A1 creates 2 times ,because u have one and create another one in A2 activity, so u can just start activity A2 without
(Intent.FLAG_ACTIVITY_NEW_TASK)
flag and than when you need to back to A1 call
A2.this.finish()
think this will help and A1 onPause will start calling
OnPause() is called when an activity is partially visible to the user but the user is not able to interact with it. In your case, when the next activity launches, the first activity calls onStop(). onPause() is only called when an alert box or a dialog bix is displayed in the screen. Try writing your code in onStop().
Now I've got the onCreate, onResume, onPause methods which are called every time I switch from A2 to A1.
This is normal because you query to recreate the activity with super.getContext().startActivity(intent);
Just call finish inside A2 to return to A1.
You can also put flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT to the Intent. The A2 activity will not be destroyed.
FLAG_ACTIVITY_NEW_TASK --> this flag will start new activity So first remove this.
Second if you want to send some data then A2 to A1 then startActivity by following method rather than startActivity()
startActivityForResult
And you will get a callback in
onActivityResult()
I have 8 Activities in my Android app and I want:
1)Every time I press Back button during my first 7 Activities to go back to my previous Activity(Act1< Act2< Act3< Act4< Act5< Act6< Act7) BUT
2)ONLY when I am in the 8th Activity I want to definitely exit my Android app and go to my phone's Home Screen.I try to do it by overriding onBackPressed method in my 8th Activity (Phone Home Screen<-Act8)
I found an Android implementation in which I insert finish();in every intent of all my 8 Activities but this is not what I want since this way I can't go back to the previous Activity whenever I want(with finish(); every current Activity is removed from back stack).
How will I do it please?
My code so far in my 8th Activity is:
#Override
public void onBackPressed()
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
Another way: create a 9th Activity and call it FinishAllActivity or something like that. Make this activity call finish() and then return in its onCreate().
In onBackPressed() in Activity 8, start FinishAllActivity using the FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK flags (see this question for more details). Activities 1-8 will be removed from the stack, then the 9th Activity will start and immediately terminate and your task stack is clear. When you reopen the app it should start from Activity 1.
The advantage of doing it this way is that you don't have to modify Activities 1-7.
Add a public static boolean to one of your classes that indicates the app is exiting. Set this boolean in activity 8 when you want the app to finish, and have all of your other activities check it in their onResume() and finish immediately if it is true. Make sure the first activity clears it before finishing, or it may still be set the next time the app runs. (Android doesn't necessarily discard the VM when your last activity finishes, so the class and its static members may be reused next time.)
Note that this is the simple way, not the "Android way." Global variables are generally frowned upon, for reasons you can Google. The "correct" way to do this would be to start each activity for result and return a result to onActivityResult(...) that indicates whether the app is exiting.
You can implement a broadcast receiver and have each of your Activities that you want to close call finish() when they receive the broadcast (which will be sent from your last activity). I would imagine you'd need to have your broadcast receiver class be either an anonymous inner class or a private class within your activity(s) so that you can easily access your enclosing Activity's finish method.
Here's a good example of broadcast receivers:
http://www.tutorialspoint.com/android/android_broadcast_receivers.htm
Look at the custom intents section.
Doing it this way is a loosely coupled way to implement what you are looking to do.
use FLAG_ACTIVITY_CLEAR_TOP Flag in your intent like below example.
Intent intent = new Intent(getApplicationContext(),FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
in your first activity check below condition.
if (getIntent().getBooleanExtra("EXIT", false)) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
}
here FLAG_ACTIVITY_CLEAR_TOP work like below example
consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.
so here you have to call D is your last activity and A is your first activity.
This way you are finishing your 8th Activity returning to your 7th Activity and same time you are like emulating a pressing of Home button on a device. When you rerun your app it will appear with 7th Activity on a screen. If you wish to see the 8th Activity in this case then just remove the finish() method. If you wish next time your app to start with 1st Activity then you should finish all the activities from 8 to 2nd but not the 1st. Also you could launch your 1st Activity adding a FLAG NEW_TASK or some other flags.
UPDATE
My advise (for the quick result without changing the workflow) is to use startActivityForResult() to start all activities in chain. When user exits the app just return a special parameter using setActivityResult() to get all nested activities know about user's choice making all nested Activities to run finish(). This way all your 8 activities will be finished properly.
I have one question in mind for activity management. Suppose I have 4 activities say for example A1,A2,A3,A4. Now A1 have one button which start activity A2. A2 have 2 buttons which start either A3 or A4, A3 have 2 buttons which start activity A4 and A1. A4 have 3 buttons to sart activity A1,A2,A3 I do not use finish method in any of this activity. So now user click any of the activity any of the button than check the activity ,that is this already in back ground? If yes than this activity would not generate new instance and start activity which is already in background. otherwise it create new insistence.
You can get this behaviour by including the FLAG_ACTIVITY_REORDER_TO_FRONT in your Intent's flags and then just calling startActivity(intent) like you normally would.
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
You can search "android:lunchMode" by Google.
Then you will get the anwser.
Whenever the button is clicked in any activity, it creates the new instance of the activity irrespective of the fact the activity is already on the activity stack. Since new Intent is fired every time, it opens new activity.
When we press back button then only it goes to the already opened activity from the stack.
I'm just learning Android programming. The way I understand it is that the services work like a stack, Is there a way for a activity to return to the first activity that started the app, instead of just the previous one.
Example, say I have 4 activities, a,b,c,d. Is there a way for activity d to have a button that would bring up activity a?? Instead of activity d going to c, and c going back to b???
You can start again activity a, but using a flag in your intent:
FLAG_ACTIVITY_REORDER_TO_FRONT
From android docs:
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.
Using this, you'll reuse the instance of activity a already running, instead of starting a new one. Note that doing this, the instances of activities b, c and d will remain in the back stack (now after activity a).
Otherwise, if you want to finish this activities (and remove them from the back stack) you can start activity a (from d) with this other flag:
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.
EDIT: this is a good resource to read, if you haven't did it yet:
Tasks and Back Stack.
Yes, you can do that. You simply code an intent for activity a to the button.
Use the command
finish():
You will be reverted to the main page
or else write an intent pointing to a
You can send information to different Activities via Intents.
Intent myIntent = new Intent(this, AvitivityName.class);
startActivity(myIntent);
Don't forget to add your Activities in AndroidManifest.xml.
Here's some tutorial on Android Intents:
how to use Android intents.
how to switch to another activity with a button click.