2017-09-22 8 views
0

Ich habe Fragmente A, B, C, die ich mit add() Methode hinzufügen.Zurück zu Backstack-Fragment

Wenn ich Fragment C erreichen, irgendwann will ich Fragment A zurück zu gehen und entfernen B und C.

Mein Ansatz:

val backStateName = FragmentA::class.java.name 
activity.fragmentManager.popBackStackImmediate(backStateName, FragmentManager.POP_BACK_STACK_INCLUSIVE) 

Ich habe auch eine specialTag hinzugefügt, um mein Fragment A, also habe ich einen Check gemacht, um sicher zu gehen, dass Fragment A immer noch im Backstack ist, bevor ich es versuche.

val fragmentToGoTo = activity.fragmentManager.findFragmentByTag(specialTag) 

und es gibt nicht Null zurück - was bedeutet, dass Fragment noch im Backstack verfügbar ist. popBackStackImmediate gibt false zurück. Warum?

Antwort

0

Wenn Sie ein Fragment anhängen (oder führen Sie eine andere Aktion sa hinzufügen/entfernen/abzulösen etc.), haben Sie die Möglichkeit, es in die Backstack mit einem Namen String hinzuzufügen:

FragmentA fragmentA = (FragmentA) fragmentManager.findFragmentByTag("A"); 
FragmentTransaction transaction = fragmentManager.beginTransaction(); 
if (fragmentA != null) { 
    transaction.attach(fragmentA); 
    transaction.addToBackStack("attachA"); 
    transaction.commit(); 
} 

Beachten Sie die " attachA "Zeichenfolge, die wir an die Methode addToBackStack() übergeben haben. Wir werden es später benutzen, um zurückzugehen. Nehmen wir an, wir haben andere Transaktionen ausgeführt - hinzugefügt/entfernt/angehängt/entfernt einige andere Fragmente. Nun, um wieder in den Zustand waren wir rufen Sie eine der popBackStack() Methoden:

fragmentManager.popBackStack("attachA", FragmentManager.POP_BACK_STACK_INCLUSIVE); 

Wenn eine Transaktion mit dem Namen „Attaché“ auf der Rückseite Stapel wurde hinzugefügt - die Methode wird uns das zurücknehmen Zustand.

In Bezug auf Ihre Frage zum Rückgabefall - Sie haben wahrscheinlich die Dokumentation über diese Methoden gelesen und welche Werte sie zurückgeben. Ich bevorzuge die popBackStack() seit

/** 
* Pop the last fragment transition from the manager's fragment 
* back stack. If there is nothing to pop, false is returned. 
* This function is asynchronous -- it enqueues the 
* request to pop, but the action will not be performed until the application 
* returns to its event loop. 
* 
* @param name If non-null, this is the name of a previous back state 
* to look for; if found, all states up to that state will be popped. The 
* {@link #POP_BACK_STACK_INCLUSIVE} flag can be used to control whether 
* the named state itself is popped. If null, only the top state is popped. 
* @param flags Either 0 or {@link #POP_BACK_STACK_INCLUSIVE}. 
*/ 
public abstract void popBackStack(String name, int flags); 

/** 
* Like {@link #popBackStack(String, int)}, but performs the operation immediately 
* inside of the call. This is like calling {@link #executePendingTransactions()} 
* afterwards. 
* @return Returns true if there was something popped, else false. 
*/ 
public abstract boolean popBackStackImmediate(String name, int flags); 
1

Ich hatte das gleiche Verhalten. Stellen Sie sicher, dass Sie für denselben Thread aufrufen, mit dem Sie es zu Ihrem Backstack hinzugefügt haben.

außerdem sicher, dass Sie .add() statt .replace()

Wie auch immer verwenden, es ist nie garantiert, dass der Backstack nicht gelöscht/zerstört, während dies zu tun. Ich habe dieses Verhalten gelöst, indem ich einfach popBackStack() benutzt habe, bis Sie das Fragment erreicht haben, das Sie haben wollen.

können Sie versuchen, so etwas wie:

fun popStack(tag: String) { 
    var isPopped = fragmentManager.popBackStackImmediate(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE) 
    if (!isPopped) { 
     fragmentManager.popBackStack() 
     //maybe a loop until you reached your goal. 
    } 
} 
+0

Tag repräsentiert Name Klasse Fragmente Tag oder Fragmente? – JoshuaMad

+1

Das Tag, das Sie zum Backstack hinzugefügt haben. In Ihrem Beispiel ist es der Klassenname –