jonathanrz
jonathanrz

Reputation: 4296

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?

Upvotes: 0

Views: 77

Answers (3)

Jave
Jave

Reputation: 31846

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.

Upvotes: 0

Zsombor Erdődy-Nagy
Zsombor Erdődy-Nagy

Reputation: 16914

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

Upvotes: 2

Maxim
Maxim

Reputation: 4214

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.

Upvotes: 1

Related Questions