Launching Activities Properly - android

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

Related

clear/finish all activities including SingleInstance activities from the stack in android

I have three activities namely A, B, and C. A is launcher activity while B and C are singleInstance activities. The sequence of movement between activities I want is A(press button) -> B and then B <-> C (i.e, B and C keeps alternating as many times as they want). When in B, pressing navigation up button takes me to activity A.
Now the problem arises. I am in A now and want to go to B(by pressing button) like I did during start of my App(i.e, I want it to work as if the app has been launched). But it tries to go to activity B and suddenly with a flash it returns back to A. The funny part now is that when I try again(i.e in activity A I press the button again) it goes to B without any problem and the flow works as expected i.e, the flow of activities are same as it was on launch time.
What I want finally?
Once I reach launcher activity which is activity A, I want the app to behave as if it was just launched. i.e all activity including singleInstance activities should be cleared from the stack.
I have tried this in onSupportNavigationUp() method in activity B:
Intent mainIntent = new Intent(B.this, A.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
B.this.finish();
I even tried adding Intent.FLAG_ACTIVITY_CLEAR_TOP also.
But this doesn't help. The problem remains unsolved.
Any kind of suggestion will be greatly appreciated.

clear all activities except one in android

Below I'm showing the flow on how I want to navigate my Activities:
I tried writing the following code inside D and E:
Intent list = new Intent(AddComplaint.this, B.class);
list.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(list);
However, I'm facing two issues:
When B gets launched it shows a grey screen for a while, then it gets loaded.
When I go back from B it exits out of the application rather than going to A (the dashbord).
How can I fix this?
I believe you can achieve what you want by using FLAG_ACTIVITY_CLEAR_TOP. If you send an Intent using this flag, it will be delivered to the existing Activity B, and any activities above B on the stack (C, D/E) will be finished.
Using FLAG_ACTIVITY_CLEAR_TASK will finish all previous activities on the stack, which would make B the only remaining activity - explaining why you exit the app when clicking back. Your grey background is unrelated to activity management and indicates the activity is simply taking a while to call onCreate().
Example code:
Intent list = new Intent(AddComplaint.this, B.class);
list.setFlags
(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(list);
I think FLAG_ACTIVITY_CLEAR_TASK is clearing all the activities from stack.
to go back from activity B to A again, without changing anything to your existing code , simple overrride onBackPress() method in activity B and startActivity A there.

Intent flags on Android

I have a widget for my application, which need to be somewhat independent from the app.
The activity workflow should be like this:
Widget -> Activity acting as receiver
Receiver -> LoginPage or Activity A (depending on login status)
LoginPage -> Activity A
Activity A onKeyDown -> Activity B
Activity B onKeyDown -> Home Screen.
I have no problem until Activity B, which sends back to Activity A when I press onKeyDown. I'm using FLAG_ACTIVITY_CLEAR_TOP flag and finishing the Activity when starting the activity B.
When I move from ActivityA to ActivityB using the CLEAR_TOP flag, I supposed that Activity stack is cleared, then in ActivityB I finish the Activity on the onKeyDown() method, assuming that the App will be closed, but it doesnt. Why?
I'm also trying to use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK in the receiver but I dont understand the mechanism pretty much. Any idea about this?
In fact the FLAG_ACTIVITY_CLEAR_TOP, start your activity B if its not started or it came back as the second activity on the BackStack. To finish Activity A, you can call finish() after starting Activity B or add no history flag, when starting A.
#JesusS: I doubt if u can finish ur activity in that fashion during a forward transition.
Consider a scenario of moving from Activity A to Activity B. Now if u want to kill Activity A and want to move to Activity B then call the startActivity(intent);
(where ur moving from activity A to B)
without any flags on the intent followed by the finish() on activity A.
As per my understanding u can use Intent.FLAG_ACTIVITY_CLEAR_TOP only during backward transition i.e when u already have that activity on the stack.
Consider the following scenario:
A --> B --> C --> D
Now if u want to move back from activity D to Activity A by clearing the activities u can go for Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP.
The result is that the Activities D, C, B(LIFO) will be removed from the stack and the activity A resumes by calling the onResume() of Activity A.

Return result to previous activity that is not the last activity

The startActivityForResult()and onActivityResult works perfect if there are only two activities are involved. But how can I handle this, if more than 2 activities exists?
Example:
Activity A starts a new activity B, that starts activity C, that starts activity D. I want to return the result of D to activity A along with finishing activities B and C. How can I do this? Can I loop through the activity stack and finish the wanted activities or must I start a new instance of activity A?
For short: A->B->C->D has to lead back to A with the result of D.
Going back closing each activity would be a good way of doing things, but if you need to jump from an activity to another and you're not using a TabHost, you could take a look at the APIDemo Reorder code
It jumps from an activity (4th) to a previous opened one (2nd) in this way:
Intent intent = new Intent(ReorderFour.this, ReorderTwo.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
In my opinion the most logical way is have the Activities take responsibility for this.
D returns d to C.
C returns d and c to B.
B returns b,c,d to A.
This will force you to consider the error conditions when the Activities don't happen in this cycle explicitly.

Activities behavior

I have four activities A,B,C,D. I am passing an text from activity A to Activity B using bundle and Activity B is displaying it nicely. Then i move from Activity B to Activity C and then Activity D. After that i called Activity B from the Activity D with the help of intent and i seeing no text are there in Activity B.
Please suggest me the way to keep the text there, with some code example.
You would need to use either FLAG_ACTIVITY_CLEAR_TOP or FLAG_ACTIVITY_REORDER_TO_FRONT.
Without one of these you are creating a new instance of your activity instead of revealing the old one. You can set activity flags on the intent you start from activity D:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
before you call startActivity.

Categories

Resources