J'ai un FragmentA qui a le bouton DoSomething et un auditeur qui est ActivityA. FragmentA définit également une InterfaceA avec la méthode doSomething () en elle.
ActivityA implémente InterfaceA et comme ci-dessous montre FragmentA (avec son bouton DoSomething dessus).
C'est le code derrière l'implémentation des boutons DoSomething cliquez dans l'écouteur ActivityA:
@Override
public void doSomething(View v) {
if (hasRight == false){
// doShowPINDialogFragment just creates and shows PINDialogFragment
// as per code below.
// Question 1: How to make doShowPINDialogFragment() return boolean?
if (doShowPINDialogFragment() == false){
return;
}
}
// If I put break point here, I would expect to see my dialog fragment
// created in doShowPINDialogFragment() call above but I dont.
// I see fragment B loaded (replacing FragmentA), then my
// PINDialogFragment shows on top of it.
// Question 2: How to prevent loading fragment B before user enters
// correct PIN?
loadFragmentB();
}
La méthode doShowPINDialogFragment () créera simplement et "affichera" le fragment:
public void doShowPINDialogFragment(String a){
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("doShowPINDialogFragment");
if (prev != null){
ft.remove(prev);
}
ft.addToBackStack(null);
PINDialogFragment pinDialogFragment = PINDialogFragment.newInstance(a);
pinDialogFragment.show(ft, "doShowPINDialogFragment");
}
Le problème est qu'Android ne bloquera pas lors de l'affichage d'une boîte de dialogue (comme dans le cas de ModalDialog en C #). En conséquence, la méthode ci-dessus s'exécutera complètement avant d'afficher mon PINDialogFragment. Je voudrais empêcher l'appel à loadFragmentB () mais l'appeler uniquement lorsque PINDialogFragment retourne true.
Mon problème est que je ne sais pas 2 choses:
Comment faire ci-dessus doShowPINDialogFragment () renvoyer vrai ou faux?
Comment empêcher l'appel à loadFragmentB () si doShowPINDialogFragment () retourne false?
Je pensais qu'écrire quelque chose comme ça résoudrait mes 2 problèmes:
@Override
public void doSomething(View v) {
if (hasRight == false){
if (doShowPINDialogFragment() == false){
return;
} else {
loadFragmentB();
}
}
}
Très appréciée.
Je l'ai compris.
Au lieu de l'écrire comme ci-dessus, faites ceci:
@Override
public void doSomething(View v) {
if (hasRight == false){
doShowPINDialogFragment();
} else {
loadFragmentB();
}
}
De cette façon, vous ne chargezFragmentB que lorsque hasRight! = False. Si false cependant, vous appelez doShowPINDialogFragment () qui crée et affiche le fragment de dialogue comme expliqué ci-dessus.
Il définit également l'interface avec un rappel qui s'exécute ensuite dans le même fichier java que le code ci-dessus. Ce rappel se déclenche sur un clic positif sur le bouton et appellera également loadFragmentB ().
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots