2015-03-11 11 views
5

Nach dem Android-Fragment Lebenszyklus würde ich erwarten, dass nach onDestroy das Fragment neu erstellt wird, oder mindestens onCreateView wird erneut aufgerufen.Android Fragment onCreateView nach onDestroy nicht wieder aufgerufen

Ich habe eine Aktivität A eine andere Aktivität B für Ergebnis und Aktivität B beginnt ein 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; 
    } 

} 

Erstellen Wenn das Fragment zum ersten Mal erstellt wird Zeit, um alles in Ordnung ist, wird die Ansicht gezeigt. Verlassen Sie die App (wie an den Systemeinstellungen zu gehen), dass die Auswirkungen hat onDestroy des Fragments ohne onDestroyView genannt wird genannt wird, aber wenn ich zurück zu meiner app onCreateView ist nicht wieder genannt, die, weil ich eine Nullpointer verursacht Ich instanziiere die Ansicht nur in onCreateView. Zurücksetzen der Ansicht in onDestroyView Ich denke, würde das Problem lösen, aber ich möchte wissen, was hier mit dem Lifecycle falsch läuft und wenn ich etwas falsch mache.

Danke.

Hier ist der Logcat-Ausgang.

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** 
+0

initialisieren Sie Ihre Ansicht in 'onViewCreated()' anstelle von 'onCreateView()' –

+0

this.currentFragment = Fragment.instantiate (this, name); Warum instanzieren Sie Ihr Fragment anstelle des Standardkonstruktors? versuche F-Fragment = new F(); –

+0

Ich denke, die startactivityForResult() könnte ein Täter sein und haben Sie Superaufrufmethoden verpasst, die Sie in Ihrem Fragment überschrieben haben? –

Antwort

1

Nachdem er einige Zeit untersuchen i „gelöst“ endlich das Problem durch die Ansicht in onCreateView zu schaffen und zerstört es in onDestroyView, ohne zu verstehen, warum das System den Rückruf nicht nennen, wie in der SDK-Dokumentation beschrieben.

Verwandte Themen