Keeping specific activity on top of a backstack - android

In our app we use an activity C from a library which always should be on the top of a back stack (or should always be visible in case if being on top is impossible).
For example we have an activity A where some asynchronous job is in progress. At some moment the activity C is launched so the backstack looks like this: A -> C. When the asynchronous job in A is done it should launch the activity B in such a way that C would still be visible but B would still exist. It looks like the back stack should be like A -> B -> C or (this looks more possible) there should be two back stacks like this: (A -> B) and (C).
For now we tried some manipulations with overriding startActivity for activity A.
Are there any better ways to achieve the situation that C should be always visible until a user closes the activity?

I have come to the answer. In case you encounter this stuff as well the solution in my case looked like this:
The idea is that I save the state in the activity A after the asynchronous work which is necessary for launching the activity B. If the activity C is on top of the stack, the activity B would not be launched (done via overriding A's startActivity method). Then when the activity C is finished, activity A’s onStart method is launched. And in this method we start Activity B. Visually it looks like what I wanted to achieve.

Related

Restoring fragment backstack for fragment with two possible backstack paths

I have one Activity with 3 fragments which form a workflow to collect user input.
Normally, Fragment A is the first fragment -> Launches B -> Launches C. B is supposed to launch A if the back button is pressed, and similarly C's Back button is supposed to launch B.
However, in some cases, A is supposed to launch C directly, and then C's back should launch A again.
I prefer that C should not know who launched it. I.e. I want C's "backstack" to operate without C knowing who launched it.
I tried using the usual addToBackstack approach, but I'm facing a problem when the Activity gets killed after the user lets the app go into the background while C was open.
I would like the user to return to "C" instead of starting all over from A. To achieve this I'm using the saved Instance state, and detecting which fragment was previously active, and launching it from the activity.
The problem starts when the user wants to go back from C, after the Activity was recreated after being killed. C doesn't know who launched it: A or B. How do I make the Back button work as expected for this case?
Found some answers in this excellent video by Android Developers + Adam Powell:
Fragments: Google I/O 2016
In summary, Fragments and the Fragment BackStack are considered part of the navigational state of the app, so, as long as I don't mess with the backstack when the activity is launched, the OS will also restore the FragmentBackStack, thus the BackStack will know who launched C (A or B) even if the activity gets re-created. Thus, popBackStack will move from C to A or B as required.

Go back to Activity without recreating (without invoking onCreate())

I would like to ask similar question to:
Go back to previous screen without creating new instance
However I don't want Activity A go through onCreate callback, I would like to return to previous instance in the same state. How to achieve that without calling multiple finish() ?
There's no need to finish() activities as you navigate through your application. Instead, you can maintain your Activity back-stack and still achieve your goal. Let's say you have 4 activites like so:
A --> B --> C -->D
where D is the topmost activity and A is root activity. If you are 'falling back' to activity B, then you'll need to do two things in order to avoid hitting B's onCreate method.
1.) Make B a "SingleTask" activity. You can do this in the Android Manifest. To be brief, this means that only one 'instance' of B will ever exist within this Task. If B is already running when it's called then it will simply be brought to the front. This is how you do that.
<activity
android:name=".ui.MyActivity"
android:launchMode="singleTask"/>
But you don't want to just bring B to the front. You want to 'fall back' to B so that your stack looks like
A--> B
2.) Add this flag to your intent that 'starts' B. This ensures that C and D are removed.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Now when 'D' calls new Intent to B, B will be resumed and C and D will be removed. B will not be recreated, it will only call onNewIntent.
When navigating from A to B do not finish Activity A.
When navigating from B to C finish Activity B.
When navigating from C to D finish Activity C.
So from wherever you navigate back finish current Activity and Activity A will get resume again not get created.

Managing activities in Android and preventing large activity stack

I'm new to Android development and I have an app with various activities. For performance reasons I'd like to properly manage the activities when users are using my app. Here's my problem.
Activity A - starting activity with map
Activity B - user navigates to Activity B (a list view) from Activity A.
The user then selects the map icon to navigate to Activity A again.
So if you can image it, my activity stack is now:
Activity A
Activity B
Activity A
So if I press the back button the device it will take forever as it scrolls through activities.
Is there a way of managing this so the old activity is destroyed and is just re-created upon choosing an activity nav icon. I've read up about onDestroy() and onStop() but I'm a little confused at their implementation.
Apologies for a poorly worded question but I'm unsure of the correct lexicon to ask about activities.
One simple solution is to kill the Activities as soon as they leave the foreground.
You could do that by calling finish() inside onPause().
You could have B finish itself and return to A instead of starting another A. Or, if your stack might be more complicated, like this:
D
C
B
A
D could start A with FLAG_ACTIVITY_CLEAR_TOP, which would cause D, C, and B to be finished, leaving A on top. (That intent flag interacts non-intuitively with a couple other flags, so read the docs.)

finish() not working properly

In my app, whenever calling the finish() method, wherever it was, I am not taken to the previous activity, rather I am directed to mainActivity.
finish();
My aim is, showing the user the activity just before the current activity he is seeing.
Question 1 : How can I make finish() always take me to the activity before ?
Question 2 : Does this work using another workaround other than finish() ?
Question 3 : How to check the stack of activities and decide accordingly which one to go to ?
If you have written finish in each intermediate activity, that means you are removing the activity from the stack, hence on finishing an activity you are taken to the last non-finished activity, hence write finish() in only that activity which you do not want to see until the same workflow is followed and its onCreate() is called
if you start activity c from b and b from a if you use the back button on your phone at activity c it will go to b and back button press in b it will show a. if you use finish in all the 3 activities what happens is a calls b and a is finished and b calls c and b is finished so when you use finish in c it will not have b to show. so you have to tell where you are placing your finish based on some condition or a button click or just before starting a new activity, post your code and we will help you.

Android: Using search API to do equivalent of startActivityForResult

I have an activity that is started from the search api. I would like it to "return" some values to the activity that was running when the search was instigated, in a similar way to startActivityForResult, but I can't see how to do it. Any suggestions?
Activity A -> (startActivity) ->
Activity B -> (Search) -> Activity C
-> (return) -> Activity B
At the moment I'm starting a new instance of activity B using the values. This works, but the activity stack is then not the way it should be. Ie, pressing back from B would go back via C, B, A, instead of just A.
Any suggestions on the way to do this?
I'd like to know how to do this too. The only kludge I can think of is to have public static variables in B to pass the result back in, and then have C call finish() after setting said variables, with B processing the results in the onResume() method.
I've come up with a solution: to combine B and C. Instead of having a separate search handling activity, I can get B to handle the search and pop up a dialog with the results in it. It doesn't feel elegant, I'd much rather have the separate, but it's the best solution I could find. This is assuming, of course, that it works, but I can't currently see a reason for it not to (in my particular situation).
EDIT: Here's the code. EditLocation is A, GetLocationMap is B
https://github.com/spookypeanut/Wake-Me-At/tree/e0dde4153c375c20bec3d7201b4faac2300f5956

Categories

Resources