Android Activity management - android

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.

Related

android activity not pausing

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

How to kill particular activity from task?

In my application I have 6 activities like 1->2->3->4->5->6. In 6th activity user can choose any activity(I am launching with intent). Means he can choose 1 or 2 or 3 or 4 or 5. Now when I press back button it is coming back to 6th activity again(which I do not want).
ex:
I have executed like this 1->2->3->4->5->6. Now I am in 6th activity. Here I have button, when I press it, it launched 4th activity. My code is like this on button click.
Intent intent = new Intent(MainScreen.this, UniversitiesScreen.class);
startActivity(intent);
So now I am in 4th activity. When I press back button it taking me to 6th activity(which I came from) instead 3rd activity(what I am expecting).
Can any one help me to handle this type of navigation.
Simple answer is edit your onBackPressed method in every activity...
For example, Write in 4th activity like this
#override
public void onBackPressed(){
Intent in = new Intent(this, ThirdActivity.class);
startActivty(in);
finish();
}
Please check the context that you are passing to intent. Every activity is different so pass the activity context from where you are calling another activity.
If my guess is true Mainscreen.this is first activity, and if you call fifth activity from fourth activity, give intent as
Intent in = new Intent(FourthActivity.this,FifthActivity.class);
startActivity(in);
finish();
And you are not saving any data in backstack and you are going back manually...
The best way is to use the NavUtils.navigateUpFromSameTask(this) function in your activity. For this you need to define the parent activity in the manifest for each activity so when you are in the 4th activity then the parent activity is the 3rd activity.Then when you click on back or navigate up you end up in the parent activity i.e. from 4th to the 3rd.
Take a look at this
http://developer.android.com/training/implementing-navigation/ancestral.html
Look at the manifest and then the Navigate up to parent task section
This will also help in returning to the same state of the 3rd activity from the 4th activity.

Control the finish of activities in android

There are two activities in my app
Activity A: Select Country
Activity B: Select Region
First I enter Activity A, select a country, then will jump to Activity B, and I would like to keep the case like this:
requirement 1 : if in Activity B press back, then go back Activity A and select country again,
requirement 2 : if in Activity B select region, then finish the Activity A
The problem is, if I startActivity() to open B, then finish Activity A , it can not meet the requirement 1, but otherwise I can not fit requirement 2.
Is there simpler way besides using onActivityResult in Activity A? Can I directly finish A in Activity B?
Thanks a lot.
When user select a region call the activity C (the one before activity A) with below code:
Intent intent = new Intent(this, activityC.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
read FLAG_ACTIVITY_CLEAR_TOP
Use have to use startActivityForResult for starting the activity
Refer the documentation on it
http://developer.android.com/training/basics/intents/result.html

OnClick restartActivity?

How can I restart An Activity on click?
For exemple: I have got in my AndroidManifest.xml 2 activities the activity A and B and they start when application starts...
But what I want is when I click in a button that is on Activity A it must restart activity B.
when you are in activity A, and proceeding to activity B, then your activity B automatically starts/re-starts
Why do you need to start both Activities on starting your app? When you say "restart", do you actually need to stop Activity B and start Activity B again? Or do you just want to show it? To start an Activity from another Activity, you could call something like this:
startActivity(new (Intent(this, ActivityB.class)));
The Android documentation gives plenty of detail. However, I think you should consider why you are starting two activities at once, and whether you might want to use a Service instead (not knowing any details of your app, I can't say).
Intent intent = new Intent(CurrentActivity.this, ActivityToLaunch.class);
startActivity(intent);
call above piece of code on onClick of view method.

android multiple instance of activity?

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

Categories

Resources