chancyWu
chancyWu

Reputation: 14423

DialogFragment doesn't show up in the listener

I know there is one question with same title, but my scenario is a bit complex. In FragmentA, once click the button will start a new activity to show FragmentB, if then click one button in FragmentB, i need to dismiss FragmentB and go back to FragmentA and show DialogFragmentC. What i do is define one listener in FragmentB and implement it in FragmentA.

The sample snippet is as below:

class FragmentA extends Fragment implements FragmentBDelegate {
    .......
    @Override
    public void onButtonClicked() {
        DialogFragment popup = new DialogFragmentC();
        popup.show(((AppCompatActivity)getContext()).getSupportFragmentManager(), null);
    }
    ......
}

class FragmentB extends Fragment{
    ......
    private void onButtonClicked(View v) {
        getActivity().finish();// to dismiss current activity
        if(mListener != null) {
            mListener.onButtonClicked();
        }
    }

    public interface FragmentBDelegate {
        void onButtonClicked();
    }

    ......
}

Why the DialogFragment doesn't show up? If listener cannot implement this requirement, how to implement?

Upvotes: 2

Views: 235

Answers (2)

chancyWu
chancyWu

Reputation: 14423

@ADM's comment guide me to one workaround. I define one showWithStateLoss() method with commitAllowingStateLoss() to commit the FragmentTransaction in DialogFragmentC. Then it works.

public void showWithStateLoss(FragmentManager manager, String tag) {
    FragmentTransaction ft = manager.beginTransaction();
    ft.add(this, tag);
    ft.commitAllowingStateLoss(); //original it's commit()
}

Upvotes: 2

Afshin
Afshin

Reputation: 9173

You should not (maybe you can, but should not) use callback method between different activities.

If this is exactly what you need to do, you need to start 2nd activity for result with startActivityForResult(). Then decide if you want to show dialog based on 2nd activity result. FragmentB will set result rather than trying to call 1st activity's callback.

Upvotes: 0

Related Questions