Ich entwickle eine App, die Fragmente verwendet, letzte Woche nahm mein Testgerät Lolipop Update. Als ich meine App auf einem Lolipop-Gerät getestet habe, habe ich gesehen, dass die Ersetzungsmethode von Fragment Transaction nicht ordnungsgemäß funktioniert.Fragments Transaktion ersetzen auf API-21 bleibt hinter
Es funktioniert mit verwirrend in Lolipop Version obwohl alles gut auf Kitkat Version.
Um meine Situation zu erklären, habe ich ein paar Bilder hinzugefügt.
- Erster Bildschirm ---------------------------- KitKat ------------- ------------------------ Lollipop -------------
Wie Sie sehen können, wenn ich kitkat
benutze, ist alles in Ordnung, aber sobald ich lolipop
Fragment Transaktion verwenden, ist das Ersetzen verwirrend.
Hier ist mein Tastencode;
mButtonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FeedbackFragment mFragmentFeedBack = new FeedbackFragment();
android.app.FragmentManager fm = getFragmentManager();
fm.executePendingTransactions();
android.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (mFragmentFeedBack.isVisible()) {
fragmentTransaction.hide(mFragmentFeedBack);
} else {
if (!mFragmentFeedBack.isAdded()) {
fragmentTransaction.replace(R.id.containerfragment, mFragmentFeedBack);
}
fragmentTransaction.show(mFragmentFeedBack);
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
hier ist meine xml;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerfragment">
</FrameLayout>
EDIT: Kitkat-Version auf einem Tablet läuft, aber ich versuchte, meine App auf dem Telefon (Kitkat-Version) Ergebnis ist das gleiche. Keine Änderungen.
Danke.
hast du in tablet (mit lollipop) die gleiche app? –
Ja. es verhält sich genauso. Beim Testen der Lollipop-Version bleibt das ersetzte Fragment zurück. – salih
Sie haben gesagt: "Ich habe gesehen, dass die Ersetzungsmethode von Fragment Transaction nicht ordnungsgemäß funktioniert". Aber es scheint mir fragmentTransaction.hide wird anstelle von replace() ausgeführt, aufgrund von Code if (mFragmentFeedBack.isVisible()). Denkst du, dass das möglich ist? Überprüfen Sie in diesem Fall KitKat mindestens. –