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.
Related
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.
I have two activities, A and B.
1) From activity A you can go to B and return to A via back button (resume).
2) From activity A you can go to B, make a selection, which will return to activity A via reloading it. Here I need to clear out the previous copy of activity A from the back stack.
In the second case I am using the following intent:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
This works, but my new activity is drawn first, then I can see the old copy getting removed - so there's some animation of the same activity going away. Is there any way to either reverse that behaviour so that the old activity is removed first, or alternatively get rid of that animation? Thanks!
EDIT:
In terms of a workaround, for now I'm setting activity A as a singleTask in manifest, overriding onNewIntent and resetting all my objects that I need to reload in onResume. That seems to be working but I'd prefer a clean slate, i.e. reload the activity
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).
I'm new to Android development and here's my problem:
I have 2 activities: A and B.
From A, I call B via startActivity(new Intent(A.this, B.class))
From B, I do some things and return to A the same way: startActivity(new Intent(B.this, A.class))
However, when on new-A I press "Back" button, first I see B class and then the old (unchanged version) of A class.
I've tried placing finish(); inside onPause() method. However, then my Activity crashes on orientation change.
How can I properly control activity backstack? Thanks in advance.
When you call startActivity without any additional flags, you will create a new instance of the Activity specified. This means that your back stack ends up looking like this:
A -> B -> A(2)
You have two options:
First option, you can add flags on the Intent that instructs it to reorder A to the front if it is already present in the back stack. Note that this will not close B; you have to call finish() in B afterward.
Intent intent = new Intent(B.this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startAcvitiy(intent);
finish();
Second option is to use startActivityForResult instead of startActivity in A and have Activity B pass a result back to A. I think this is much cleaner and it gives you a very easy way to react to what happened when B was finished (e.g. you can update your UI). See this guide from the Android developers site.
Either StartActivityForResult or think about using fragments. If you need to jump between activities think about why you are doing this? If you are passing info between the two then there are a couple of options. Fragments give you the option and you can hold the data on the Activity that hosts the fragments or you can have a Singleton living in an Application class that you would extend.
Read about activity lifecycle - here. You have to implement saving of instance.
It sounds like that rather than calling startActivity in to go back to A, you can just call instead finish();. This will land you back on your previous Activity.
Alternatively you can accomplish the same if you use FLAG_ACTIVITY_CLEAR_TOP:
Intent intent = new Intent(B.this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Regarding the accepted answer's suggestion to use FLAG_ACTIVITY_REORDER_TO_FRONT - for your case it really doesn't matter. but in a slightly different case where you have A->B->C and you use REORDER to go back to A, you'd actually end up with a stack that looks like B->C->A. With CLEAR_TOP, you'd end up with just A. Point is, REORDER is meant to REORDER, CLEAR_TOP is supposed to pop the backstack. In your simple case they happen to accomplish the same, but in my opinion it's better practice to use the flags as they're intended.
I am having trouble popping all activities off the stack using Intent.FLAG_ACTIVITY_CLEAR_TOP and android:launchMode="singleInstance".
In my application activity A, launches activity B (via startActivity) which in turn launches activity C (via startActivity). On activity C the user presses a menu item to return to activity A. When they arrive at activity A, I want only A on the stack such that if they click the back button they return to the home screen (desktop).
This is the code that I am currently using when the user presses a button to return to A:
Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
All activities are defined as android:launchMode="singleInstance" in the project manifest.
My code doesn't seem to work though. Once I'm back on activity A if I click the back button I return to activity C. Am I misunderstanding how to use Intent.FLAG_ACTIVITY_CLEAR_TOP?
I've always found the best way to ensure C would be removed from the stack is to call finish() after startActivity to remove C from the stack.
The documentation does read as though things would behave the way you expected them to, but it would seem this isn't happening, so finish() will ensure C is removed.
I usually use the technique Al suggested (calling finish() after starting the new activity).
You could also experiment with task affinity. I've never done that myself, but it may be relevant in your case as well. See this thread: http://groups.google.com/group/android-developers/browse_frm/thread/ca3b26a14d024597/129e37375105901b