DialogFragment callback on orientation change

前端 未结 5 459
执笔经年
执笔经年 2021-02-01 20:40

I\'m migrating my dialogs, currently using Activity.showDialog(DIALOG_ID);, to use the DialogFragment system as discussed in the android reference.

相关标签:
5条回答
  • 2021-02-01 21:18

    Yeah, this is a common trap I'm falling in all the time myself. First of all let me say that your solution of calling DialogTest.udateListener() in onResume() seems to be fully appropriate to me.

    An alternative way would be to use a ResultReceiver which can be serialized as a Parcelable:

    public class DialogTest extends DialogFragment {
    
    public static DialogTest newInstance(ResultReceiver receiver, int titleId, int messageId) {
        DialogTest frag = new DialogTest();
        Bundle args = new Bundle();
        args.putParcelable("receiver", receiver);
        args.putInt("titleId", titleId);
        args.putInt("messageId", messageId);
        frag.setArguments(args);
        return frag;
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int titleId = getArguments().getInt("titleId");
        int messageId = getArguments().getInt("messageId");
        ResultReceiver receiver = getArguments().getParcelable("receiver");
    
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // dialog title
        builder.setTitle(titleId);
        // dialog message
        builder.setMessage(messageId);
    
        // dialog negative button
        builder.setNegativeButton("No", new OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                receiver.sendResult(Activity.RESULT_CANCEL, null);
            }});
        // dialog positive button
        builder.setPositiveButton("Yes", new OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                receiver.sendResult(Activity.RESULT_OK, null);
            }});
    
        // create the Dialog object and return it
        return builder.create();
    }}
    

    Then you can handle everything in the Receiver like this:

    protected void onReceiveResult(int resultCode, Bundle resultData) {
        if (getActivity() != null){
            // Handle result
        }
    }
    

    Check out ResultReceiver doesn't survire to screen rotation for more details. So in the end you probably still need to rewire the ResultReceiver with your Activity. The only difference is that you decouple the Activity from the DialogFragment.

    0 讨论(0)
  • Another way is that you can stop the activity getting recreated. You have to tell Android that you'll handle the orientation change yourself and android won't recreate your activity. You need to add this for your activity to your manifest file:

    android:configChanges="keyboardHidden|orientation"
    

    If not this, then you can use standard onSaveInstanceState() to save your state and recover using savedInstanceState as recommended by Google.

    Here's Google's official guide for it: http://developer.android.com/guide/components/activities.html#Lifecycle

    Go through it if you haven't already. It'll really help you in android development.

    0 讨论(0)
  • 2021-02-01 21:31

    There is better solution instead of using static methods and variables because it would work only fro one instance of your dialog. It is better to store your callback as non static member

    private DialogTestListener mListener;
    public void setListener (DialogTestListener listener){
      mListener = listener;
    }
    

    Then you should show your dialog using TAG like this mDialogFragment.show(getSupportFragmentManager(), DIALOG_TAG);

    And then in onResume method of your activity you can reset your listener

    protected void onResume() {
       super.onResume();
       mDialogFragment = (CMFilterDialogFrg) getSupportFragmentManager().findFragmentByTag(DIALOG_TAG);
       if(mDialogFragment  != null){
           mDialogFragment.setListener(yourListener)
       }
    }
    
    0 讨论(0)
  • 2021-02-01 21:36

    While André's solution works, a better solution is to get the updated activity during onAttach() in your Fragment.

    private DialogTestListener mListener;
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mListener = (DialogTestListener) activity;
    }
    

    With this solution, you won't need to pass the Activity in newInstance() anymore. You just need to make sure the Activity owning your Fragment is a DialogTestListener. You also don't need to save the state like in the ResultReceiver solution.

    0 讨论(0)
  • 2021-02-01 21:39

    First, call setTargetFragment from FragmentParent to start dialogFragment. In dialogFragment use getTargetFragment to callback fragment and return data. All data result will excute in onactivityresult of FragmentParent

    follow this link: Receive result from DialogFragment

    0 讨论(0)
提交回复
热议问题