Control the finish of activities in android - 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

Related

Android - How do I switch between 1st and 3rd activities?

I have a 1st and a 3rd activity. I wish to push a button on the 3rd activity and it finishes the 3rd and 2nd activity switching all the way back to the 1st activity(it's the MainActivity might I add). In other words, is there a way to finish automaticly the call stack of activities to a specific activity?
You can use method startActivityForResult() to start your activity.
You need to call method setResult() when you finish the activity.
And override method onActivityResult() to do what you want to do.
Actually, seems 2 methods can solve this.
Assuming Activities: A -> B -> C, and all alive in the same stack.
1) Use android:launchMode="singleTask" for Activity A. When C calls A, A starts and both B & C finish.
2) When starting A, adding flag Intent.FLAG_ACTIVITY_CLEAR_TOP.
if you don't have to pass any data to the main activity, i guess this is what you are looking for: how back to the main activity in android?
execute this code at the button click in your 3rd activity:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this code clears your activity stack and reopens your main activity.

Launching Activities Properly

I have an app that has multiple activities, one activity opens the other and so on. I have a serious problem when it comes to returning to the previous activities. I want to return to the previous activity in the state I left it in(I do not want to recreate the activity). I was able to do so with three activities, but the fourth activity skips the third activity and returns to the second, for example:
Activity A -> Activity B -> Activity C -> Activity D
What I want when I press the back button and the Up button:
Activity A <- Activity B <- Activity C <- Activity D
I initiate Activity A as a "singleTask", then I launch the next three activities like this:
Intent intent = new Intent(context, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
startActivity(intent);
This works perfectly with Activity B and C, but when I get to Activity D and try to return to Activity C, it takes me to Activity B instead of Activity C.
I have been through the internet and I just come seem to really understand the use of Intent flags and activity launch modes. Can someone please assist, pretty please?
Using the launch modes and flags IS the problem. Maybe you'll want to assign activity A "singleTop", but you don't need to use flags when launching B, C, or D (at least not in the situation you're describing). Then pressing back will behave as you expect it to (unless you're overriding it).

How to manipulate current task's Activity back stack?

Currently I have 3 Activity classes A, B and C.
Activity A is a singleTask while other has the default launch mode.
Consider a case: the user is first in A, then starts B, and then starts C.
The back stack now is ABC.
Next, the user starts A again.
The back stack now is A, but what I would like to be achieved is ABCA.
I know not setting Activity A to be singleTask can have a back stack : ABCA.
But I really need the Activity A to be the same instance.
Anyone know how to do this?
You have stipulated two conditions:
what I would like to be achieved is ABCA.
and
I really need the Activity A to be the same instance.
These two conditions are mutually contradictory. Absolutely.
What you want is impossible.
That is all.
Are you sure you need singleTask and not singleTop ? Could you describe why do you need it singleTask ?
That is not possible.
if you want to open existing activity then
launch your activity as
Intent intent = new Intent(this, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

Android Activity management

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.

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.

Categories

Resources