Hunt
Hunt

Reputation: 8425

abnormal behavior of parent activity after finishing child activity

I have Activity A and I am calling Activity B from Activity A using setResultForActivity.

Now in Activity B when I press Done button I am firing finish() and it returns to Activity A and it return down to onActivityResult. Now the issue is after when I fired finish() in Activity B , Activity A's onCreate doesn't get called and thats why

some of the custom listeners in my ListView isn't working , it seems that they are not bind.

so the whole activity respond pretty weirdly , can anyone has solution to this ?

Upvotes: 0

Views: 1167

Answers (5)

Ambar Jain
Ambar Jain

Reputation: 517

Just place your onCreate() stuff in onResume() of Activity A except setContentView().

Upvotes: 1

class stacker
class stacker

Reputation: 5347

Why a fourth answer? Because in my view, the others aren't fully correct.

Truth is, Activity A could have been destroyed in the meantime, or not. This depends on whether Android needs memory or not. So it is possible that Activity A´s onCreate() is called (along with the other lifecycle callbacks), or not. In the latter case, onActivityResult() is called before onResume().

While for configuration changes, the most efficient way to preserve the Activity's state is via nonConfigurationState, if you want to prepare for a restart of your Activity after it has been destroyed, you can use the InstanceState mechanism, because while Android destroys your Activity A, it will keep its intent and saved instance state to re-crearte it.

This stresses the absolute necessity to place initialization exactly in the callback where it belongs.

To test whether your Activity logic works regardless of whether Android destroyed it or not, you can use the DevTools setting "Development Settings" -> "Immediately destroy activities". The DevTools app is available on AVDs and can also be downloaded from Google Play.

Upvotes: 2

Entalpi
Entalpi

Reputation: 2033

After your child activity is finished() it return to execute onActivityResult which is in your case in Activity A. The onCreate method is not supposed and does not get called when killing of you sub-activity, a.k.a Activity B.

Please post some source code for us to work on and I will improve my answer! :)

Upvotes: 0

John Leehey
John Leehey

Reputation: 22240

Activity A's onCreate won't get called because the activity has not been destroyed. When an Activity regains focus from another activity, it's onStart and onResume get called, so I would put your bound listeners in those. They will also be called when onCreate is normally called.

Upvotes: 0

ruben
ruben

Reputation: 1750

Just have a read on Android Activity Lifecycle : http://developer.android.com/training/basics/activity-lifecycle/stopping.html. onCreate() is only called when the activity is first created. You can do your list thing in the onResume() method.

Upvotes: 0

Related Questions