Android Multiple Screen Navigation - android

I have 3 activities to navigate. like Activity A, B, C.
From A to B i called startActivityForResult() method and overwrite onActivityResult() method. In B activity onBackPressed() i set the setResult() method and it is working fine.Now from B to C activity I again called startActivityForResult() and overwrite onActivityResult() in B and in C when back pressed I set setResult() and then call finish().
Now the issue is when I back pressed from C it will directly going to the A. I want the back navigation from C-->B--->A
Kindly help me to achieve this.

Dont call finish() in Activity C.
onBackPressed() finishes your activity. Beacuse of finish() you are going back one more step.
Remove call to finish() and everything should work as you expect.

Related

Parent activity onCreate() called on back press (Android)

I have 3 activities - A, B and C.
From A I start B, and from B I start C, i.e
A -> B -> C.
On going from A to B, I deliver an intent to B - let's call this intent intentA.
When I go from B to C, and then from C to B by pressing back button, 'onCreate()' of B is called again and intentA is delivered to B again, even though I didn't pass any intent to B while pressing back button in C.
What do I do so that 'onCreate()' of B is not called when pressing back button in C, or even if it is called, intentA is not delivered to it?
I tried setting launchmode of B to 'singletop'/'singletask' in manifest, but they didn't work.
I tried using 'finish()' in onBackPressed() method in C, but didn't work.
I tried other solutions as well using answers to similar questions on stackoverflow, but none of them worked.
Any solution?
Also, I'm using Dexter library to ask for permissions in my custom dialogbox, and I'm using screen transition animations as well. Could these be causing the issue?
For launching ActivityC from ActivityA with adding ActivityB to backstack,
use startActivities with array of intentA and intentB as parameter

Android back stack

I have an application with several Activities in Android A,B,C,D. I have a question about the back stack.
A->B->C->D it is a normal sequence.
My question is : when i press the back button in Activity D, i have to go to the Activity B, not back to ActivityC. Since ActivityC have some imageView i want to save, i don't want to use noHistory to destroy the ActivityC. it is possible to do that
A1->B1->C1->D1
the back stack should be: D1->B1->A1 . C1 saved some imageView and if C1 launch again, the imageView in C1 will contain the same images. Is it possible just modify the code in ActivityC??
thank you
You should call finish() in activity C after, starting of activity D
startActivity(intent);
finish()
Handle onBackPressed() of each activity. So that after back pressing on D, it will redirect to B, then similarly to A.
Use onActivityResult() methods of an activity:-
Here is how it will work -
Use startActivityForResult() method while you are starting activity
While when you are going back
Activity D - before finishing write -
Intent i = getIntent();
setResult(RESULT_OK);
finish();
Your D is finished and c's onActivityResult() will be called - save your data and call finish() with above methodology, It will go to B's onActivityResult().

Resume Parent Activity

I have 2 Activity A and B, A calls B so, I want to Resume parent Activity (A) when B calls finish() on its Activity. Any advice will be useful.
UPDATE:
Maybe I should mention that I use fragments, each Activity has its own fragment, I call finish() from fragment hosted inside B activity and I expect to receive Resume on fragment belongs to A.
When you call Activity B, Activity A will go to background. Unless you finish Activity A while starting Activity B, it will automatically resume when Activity B finishes.
If you are calling this,
startActivity(ActivityB.class, this);
finish();
Just remove the finish(). It should work as expected.

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.

Activities lifecycle in Android

I have an application that have three activities, lets call they A, B and C for convention.
A calls B with StartActivity.
When user hit the back/cancel button, I have to call Activity C, so I implemented in OnPause of Activity B to call Activity C and I need return from activity C, so I called Activity C with startActivityForResult and implemented the method onActivityResult in Activity B to get the return.
Everything is working fine, but when activity C finishes, the application is getting back to Activity A, and I need Activity B.
I have to call Activity B explicitly again or I made something wrong?
I'm really not sure what you want to achieve with this behaviour. Anyway, you get back from C to A, because you pressed the Back key, and didn't override it's behaviour in onBackPressed(). So the current Activity (B) just got finished, hence onPause() was called, so C started. But by the time C becomes active, you'll only have A and C on the Activity stack.
You need to override onBackPressed() in Activity B, and call C from there, forget onPause().
You shouldn't call another activity on hitting back button. Back button will pop activity(B) out of the stack. When you clicked back from Activity B Android will finish that activity and kill it. This is a standard workflow which better do not mess up. Place some button in activity B and call C for result from there, then you will be able to get a result in B activity.
When you get to the onPause() in activity B, it is already shutting down, so when you return to it, it will be gone.
You can either, as you say, start B from C when done, or start C in B's onBackPressed() (and then return from that method without calling the super method). This overrides the default action to shut down the Activity.

Categories

Resources