Android and popping from Activity C to Activity A or B - android

I have three activities in my activity stack, A (main) -> B -> C.
A starts B starts C.
My C is a dialog box with one button that will take me to A and one button that will take me to B. The Activity B must therefore have history, so if C calls finish() I will always end up in B.
Given this, how do I (efficiently) set this up? i.e. how do I (efficiently) go from C to A?

I assume that you want to finish() B if the user selects the A button on C. Have B start C for a result (startActivityForResult()) and have C send back to B the result. If the result is "go to A", then B can just finish().

One way is to set up the onActivityResult method in activity B.
Based on the button clicked, return from activity C with different return codes, which you can access in the onActivityResult method of activity B. Based on the return code, you can either stay on the activity B, or close the activity which will take you to activity A.

Related

startActivityForResult usage for 3 activity Android

Hey guys I have 3 activity,
Activity A -> Activity B -> Activity C
This flow I want to perform in my application. I want this scenario in my Activity C
Scenario 1
On Back press from Activity C to open Activity B then again back press then open Activity A
Scenario 2
When something submit on Activity C, I want to open Activity A and I want to hide Activity B
So how can I do this through startActivityForResult. I check through this stackoverlow, but nothing works. Thanks
I'm assuming that the 3 activities are opened in order A -> B -> C, so they are in the backstack in that order. You can check the return values from Activity C, and act accordingly.
In Activity C, setResult(Activity.RESULT_CANCELLED) during onCreate(). That will make it the default return value when activity C is closed (back press or otherwise)`.
Then setResult(Activity.RESULT_OK) whenever something is submitted. After that, you can fininsh() that activity.
Whenever Activity C get closed, in Activity B onActivityResult(), check the return value. If it's RESULT_OK, call finish() (which will return to Activity A). Otherwise, do nothing, so Activity B will stay open

Return on activity A since end activity D with keep back on activity B ans C

I have 5 activities (A, B, C and D).
A -> B -> C -> D
On activity B and C, if we press the return button, I would like the previous activity to be called (respectively A and B).
Activity D is an end activity without a return button.
When I use "finish ()", it's currently going back to Activity C.
I would like to go back directly to activity A when we reach activity D by executing finish() without going through B and C.
However since B I must always be able to return to A and too C can back to B.
"A" is an activity in launchMode singleTop.
android: noHistory = true on activity D does not allow me to return directly to A.
The only solution I have found for the moment is to use startActicity (A) instead of "finish ()" but I find that this solution is not a good pararique.
Do you have a solution to my problem?
In your D activity use this:
Intent i = new Intent(this, YourFirstActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
This way, you'll return exactly to your A activity.

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.

Android finish parent Activity

I have 3 different activities in a TabGroupActivity. Let's say A - Parent, B - Child 1, C - Child 2.
A --- starts --> B
B --- starts --> C
and I have an alert dialog in C which shows some message. I want to go back to activity A when I press Ok button on dialog.
But the problem is that I can open activity C from other activities too and I want to go back to their parent activities too. So I need to make something which will work no matter which activity opens C. I've tries with this one but didn't work :
Intent intent = new Intent(Synchronization.this,Synchronization.this.getParent().getClass());
but it didn't help me. Any suggestions?
You just have to make use of two Activity methods viz. startActivityForResult() and onActivityResult()
Example : http://www.vogella.de/articles/AndroidIntent/article.html#explicitintents
Here goes the logic :
In ActivityB
Start ActivityC by using startActivityForResult(activityCIntent,INT_CODE);
In ActivityC
Now check if Dialog's OK Button is pressed, if yes then set the result using setResult(RESULT_OK,intent); and then call finish();
Then control will be redirected to ActivityB's onActivityResult() method.
Now inside onActivityMethod() check whether result_code==RESULT_OK and requestCode = INT_CODE. If yes then simply call finish();
In the Activity B start the C Activity as startActivityForResult() so when you finish the C activity it will back to the B with result. As the result you may pass flag with the intent object.
Now when you finish the C activity with ok button then set the result as RESULT_OK into the setResult() if you need to pass the data back to the B activity you may set the data into the Intent add this intent with the setResult() method and then finish the C Activity.
Now in B check the requestcode is from the C then finish this Activity. As you start this C Activity you can also start the B Activity for the A Activity.
And you need to override onActivityResult() in B activity and if you start the B Activity as for result then also you need to define also into A Activity

How to clear current activities in the stack?

Support App starts activity A, then A starts activity B and finishes itself. After that activity B starts activity C.
Now the stack contains B and C, with C at the top.
Then I click a button in activity C, and want it to clear B and C and start activity A, i.e. I want activity A to be the only activity in the stack. How can I make it?
Edit: I made a test to use FLAG_ACTIVITY_CLEAR_TOP. But it didn't work in my case, because activity A is not running when button in activity C is clicked.
Set the FLAG_ACTIVITY_CLEAR_TOP flag on your intent to start activity A.
Edit: Is there a reason you can't leave A going? Then you could do as suggested.
Otherwise, another (more complicated) option:
In B start C forResult. When A is started from C, you could finish C with a result indicating to B to also exit.

Categories

Resources