Fragment.isAdded gibt true zurück, nach fragment aus einem container entfernt
Habe ich eine Aktivität mit dem unter layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="@+id/frameLayoutA"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>
<FrameLayout
android:id="@+id/frameLayoutB"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
In onCreate der activity lade ich Fragment_A in frameLayoutA und Fragment_B in frameLayoutB.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fmA=(FrameLayout) findViewById(R.id.frameLayoutA);
fmB=(FrameLayout) findViewById(R.id.frameLayoutB);
fragA=new FragmentA();
fragB=new FragmentB();
fragC=new FragmentC();
addFragmentsInActivity(R.id.frameLayoutA,fragA);
addFragmentsInActivity(R.id.frameLayoutB,fragB);
}
public void addFragmentsInActivity(int id, Fragment fragment)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(id, fragment);
fragmentTransaction.commit();
}
Über ein Menü operation, die ich laden will Fragment_B in frameLayoutA und Fragment_C in frameLayoutB. Die Bedienung der Menüs ist:
removeFragmentsInActivity(R.id.frameLayoutB,fragB);
addFragmentsInActivity(R.id.frameLayoutB,fragC);
if(!fragB.isAdded()){
Log.e("check", "fragB already removed from frameLayoutB");
removeFragmentsInActivity(R.id.frameLayoutB,fragB);
addFragmentsInActivity(R.id.frameLayoutA,fragB);
}
else{
Log.e("check", "fragB already added");
}
public void removeFragmentsInActivity(int id, Fragment fragment)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragment);
fragmentTransaction.commit();
}
Fragment_B wird nicht angezeigt, in frameLayoutA. frameLayoutA zeigt Fragment_A. Bei der Menü-Bedienung ist wieder geklickt die Fragment_B geladen wird.
Debuggen habe ich festgestellt, dass nach fragB.isAdded() gibt true zurück, nach fragB.die remove () - operation durchgeführt. Im 2. Menü-Bedienung fragB.isAdded() return false und fragB.add() ausgeführt wird und FragmentB ist gezeigt in frameLayoutA.
Ich verstehe Begehen, ist eine asynchrone operation. isAdded true zurückgeben, da Begehen ist async und entfernen operation commit ist nicht getan, bis die Zeit fragB.isAdded() aufgerufen wird. Ist es wahr?
Freundlich darauf hin die mögliche Lösung oder Ansatz, um das Problem zu lösen.
Grüße,
Vibhor
InformationsquelleAutor vibhor | 2012-05-23
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ja die Transaktion asynchron. Wenn Sie möchten, um sicherzustellen, dass alle trasactions beendet haben, bevor die Ausführung
isAdded
führen:Aus der Dokumentation für
executePendingTransactions()
:Sodass Ihr code sollte wie folgt Aussehen:
Beachten Sie auch Fix entfernen des fragments A.
InformationsquelleAutor Paweł Nadolski
vielleicht können encapture commition von FragmentTransaction wie diese
commitNow() und commitNowAllowingStateLoss() Hinzugefügt wird in API-level 24
Aufrufen commitNow vorzuziehen ist, bei Aufruf von commit() auf, gefolgt von executePendingTransactions() als die letzteren haben den Nebeneffekt, zu Begehen versuchen alle aktuell ausstehenden Transaktionen, ob das das gewünschte Verhalten ist oder nicht.
InformationsquelleAutor user1611552