6

According to the Android fragment lifecycle i would expect that after onDestroy the fragment will be recreated, or at least onCreateView is called again.

I have an Activity A starting another Activity B for result and Activity B creating a fragment F.

public class A extends FragmentActivity {
    ...
    public void onButonClick() {
       Intent intent = new Intent(this, B.class);
       startActivityForResult(intent, REQUEST_B);
    }
}

public class B extends FragmentActivity {
    ...

    public void onCreate(Bundle savedInstanceState) {
            ...
            this.currentFragment = Fragment.instantiate(this, name);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(this.view.getFragmentContainerId(), this.currentFragment, taskName);
            transaction.commit();
    }
}

public class F extends Fragment {
   @override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       this.view = new MyView();  
   }

   @override
   public void onResume() {
       this.view.doSomething();  
   }

   @override
   public void onDestroy() {
       this.view = null;
   }

}

When the Fragment is created first time everything is ok, the view is shown. Leaving the app (such as going to system settings) has the affect that onDestroy of the fragment is called without onDestroyView is being called, but when i go back to my app onCreateView ist not called again which causes a NullpointerException because i am instantiating the view only in onCreateView. Resetting the view in onDestroyView i think would solve the problem, but i want to know what's going wrong here with the lifecycyle and if i am doing something wrong.

Thanks.

Here is the logcat output.

    03-11 11:22:47.565    6594-6594/com.xy.android.app I/ActivityA Perform button click.
    03-11 11:22:47.595    6594-6594/com.xy.android.app V/ActivityA Pausing activity
    03-11 11:22:47.605    6594-6594/com.xy.android.app D/ActivityB Creating activity
    03-11 11:22:48.075    6594-6594/com.xy.android.app V/ActivityB Starting activity
    03-11 11:22:48.105    6594-6594/com.xy.android.app I/ActivityB Resuming activity
    03-11 11:22:48.476    6594-6594/com.xy.android.app I/ActivityB Starting task FragmentF.
    03-11 11:22:48.536    6594-6594/com.xy.android.app I/FragmentF Attached to activity.
    03-11 11:23:02.350    6594-6594/com.xy.android.app I/FragmentF Creating fragment
    03-11 11:23:02.390    6594-6594/com.xy.android.app I/FragmentF Creating view for fragment
    03-11 11:23:02.420    6594-6594/com.xy.android.app V/FragmentF View for fragment created
    03-11 11:23:02.430    6594-6594/com.xy.android.app D/FragmentF Activity created.
    03-11 11:23:02.441    6594-6594/com.xy.android.app V/FragmentF Starting fragment
    03-11 11:23:02.741    6594-6594/com.xy.android.app V/ActivityA Saving activity instance state.
    03-11 11:23:02.761    6594-6594/com.xy.android.app I/ActivityA Stopping activity
    03-11 11:23:07.686    6594-6594/com.xy.android.app V/FragmentF Pausing fragment.
    03-11 11:23:07.696    6594-6594/com.xy.android.app V/ActivityB Pausing    activity
    03-11 11:23:08.517    6594-6594/com.xy.android.app D/FragmentF Save instance state.
    03-11 11:23:08.567    6594-6594/com.xy.android.app D/ActivityB Saving activity instance state.
    03-11 11:23:08.597    6594-6594/com.xy.android.app I/FragmentF **Destroying fragment**
    03-11 11:23:08.627    6594-6594/com.xy.android.app I/ActivityB Stopping activity
    03-11 11:23:14.033    6594-6594/com.xy.android.app V/FragmentF Starting fragment
    03-11 11:23:14.043    6594-6594/com.xy.android.app V/ActivityB Starting activity
    03-11 11:23:14.063    6594-6594/com.xy.android.app I/ActivityB Resuming activity
    03-11 11:23:14.063    6594-6594/com.xy.android.app I/FragmentF **Resuming fragment**
Dokumans
  • 359
  • 1
  • 3
  • 14
  • initialize your view in `onViewCreated()` instead of `onCreateView()` – Shayan Pourvatan Mar 11 '15 at 09:16
  • this.currentFragment = Fragment.instantiate(this, name); Why are you instantaining your fragment like this instead of default constructor? try F fragment= new F(); – Vigneshwaran Murugesan Mar 11 '15 at 09:21
  • I guess the startactivityForResult() may be a culprit and did you missed super calls methods you overridden in your fragment? – Vigneshwaran Murugesan Mar 11 '15 at 09:34
  • From Fragment lifecycle, can be return to `onCreateView` after `onDestroy`? from `onDestroyView` can return to `onCreateView`. – Xcihnegn Mar 11 '15 at 09:51
  • @shayanpourvatan what will this cause? can you please provide more details. – Dokumans Mar 11 '15 at 10:15
  • @Vigneshearan.m i checked all overriden methods, i am always calling super calls. I think the way creating a fragment shouldnt be the problem for the lifecycle ? – Dokumans Mar 11 '15 at 10:16
  • Since the code is not complete could you please attach the Logcat here? @Dokumans – Vigneshwaran Murugesan Mar 11 '15 at 10:19
  • override onDestroy of your Activity B. – Rajen Raiyarela Mar 11 '15 at 10:37
  • @RajenRaiyarela can you please give more detail in your answer. What should i do in the overriden method? By the way `onDestroy` is already overriden but it is not called (correctly) because the activity is only stopped. – Dokumans Mar 11 '15 at 11:01
  • nothing has to be done, just override ondestroy in your activity class also. – Rajen Raiyarela Mar 11 '15 at 11:09
  • But onDestroy of the activity is not called, how will an empty onDestroy affect the lifecycle of the fragment if it is not called ? – Dokumans Mar 11 '15 at 11:12
  • check this http://stackoverflow.com/a/21668806/3831557. as per it - if Activity's onDestroy() not called, fragment's onDestroy() won't be called. – Rajen Raiyarela Mar 11 '15 at 11:17
  • @RajenRaiyarela I am already aware of the link, thanks. Did you have a look at the logcat i provided? `onDestroy` of ActivityB is not called, but `onDestroy` of Fragment and afterwards only `onStart` and `onResume` of the activity and fragment. – Dokumans Mar 11 '15 at 12:21

1 Answers1

3

After investigating some time i finally "solved" the problem by creating the view in onCreateView and destroy it in onDestroyView, without understanding why the system does not call the callback as described in the sdk documentation.

Dokumans
  • 359
  • 1
  • 3
  • 14