0

I have a FragmentA which has button DoSomething and a listener which is ActivityA. FragmentA also defines an InterfaceA with method doSomething() in it.

ActivityA implements InterfaceA and as below shows FragmentA (with its button DoSomething on it).

This is the code behind implementation of DoSomething buttons click in ActivityA listener:

@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();  

}

Method doShowPINDialogFragment() will simply create and "show" 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");
}

Problem is that Android will not block when showing a Dialog (like in case of ModalDialog in C#). As a result, the method above will execute completely before showing my PINDialogFragment. I would like to prevent call to loadFragmentB() but call it only when PINDialogFragment returns true.

My problem is that I dont know 2 things:

  1. How to make above doShowPINDialogFragment() return true or false?

  2. How to prevent call to loadFragmentB() if doShowPINDialogFragment() returns false?

I was thinking that writing it something like this would solve my 2 problems:

@Override
public void doSomething(View v) {
  if (hasRight == false){
    if (doShowPINDialogFragment() == false){ 
        return;
    } else {
      loadFragmentB();
    }
  }
}

Much appreciated.

pixel
  • 9,653
  • 16
  • 82
  • 149

1 Answers1

0

I figured it out.

In stead of writing it as above, do this:

@Override
public void doSomething(View v) {
  if (hasRight == false){
    doShowPINDialogFragment();
  } else {
    loadFragmentB(); 
  } 
}

This way, you loadFragmentB only when hasRight!=false. If false however, you call doShowPINDialogFragment() which creates and shows dialog fragment as explained above.

It also defines interface with a callback that is then executes in same java file as code above. That callback fires on positive button click and will also call loadFragmentB().

pixel
  • 9,653
  • 16
  • 82
  • 149