Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragment onCreateView after onDestroy not called again

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**
like image 293
Dokumans Avatar asked Mar 11 '15 09:03

Dokumans


People also ask

What happens to fragment when activity is stopped?

onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity. onDestroyView() allows the fragment to clean up resources associated with its View. onDestroy() called to do final cleanup of the fragment's state.

How can we prevent onCreateView when back button is pressed in fragment in Android?

Create an init() method where you do all initialization and server calls and logically branch out call to init() method whenever you don't want to call init().

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.

Is onCreate or onCreateView called first?

onCreate() is called to do initial creation of the fragment. onCreateView() is called by Android once the Fragment should inflate a view. onViewCreated() is called after onCreateView() and ensures that the fragment's root view is non-null .


1 Answers

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.

like image 54
Dokumans Avatar answered Oct 03 '22 03:10

Dokumans