MkhytarMkhoian / conductor-dialog

This repository is not more developing. Now it's part of https://github.com/lalafo-dev/Conductor
19 stars 0 forks source link

View state of dialog saved but never restored #3

Open ursusursus opened 6 years ago

ursusursus commented 6 years ago

Hi, restoring state of controller works (i.e. after rotation dialog stays (comes up again) shown)

However if I put a view inside my alert dialog (usecase: title, edittest, cancel button, ok button) then the text inside edittext is not restored after config change. DialogFragment does this, so I presume this is a bug

I looked around the source and the problem seems to be that you do save the dialog instance state at line 237. However you never restore it. Looking at the DialogFragment is should be after the

dialog.setOwnerActivity(getActivity()); dialog.setCancelable(cancelable); dialog.setOnShowListener(this); dialog.setOnCancelListener(this); dialog.setOnDismissListener(this);

combo of setters.

I would restore it myself, but you made onBindView private and onCreateView final

(Btw looking around with debugger, onRestoreInstance state was never called for me after rotations, is this also as designed?)

Workaround for now is to manually do it via

override fun onRestoreViewState(view: View, savedViewState: Bundle) {
        super.onRestoreViewState(view, savedViewState)
        editText?.onRestoreInstanceState(savedViewState.getParcelable("foo"))
    }

    override fun onSaveViewState(view: View, outState: Bundle) {
        super.onSaveViewState(view, outState)
        val foo = editText?.onSaveInstanceState()
        if (foo != null) {
            outState.putParcelable("foo", foo)
        }
    }
ursusursus commented 6 years ago

Okay now I noticed that savedViewState always empty in onBindView, and only non-empty if I put something in onSaveViewState

@Override protected void onRestoreViewState(@NonNull View view, @NonNull Bundle savedViewState) {
        super.onRestoreViewState(view, savedViewState);
        Bundle dialogState = savedViewState.getBundle(SAVED_DIALOG_VIEW_STATE_TAG);
        if (dialogState != null && dialog != null) {
            dialog.onRestoreInstanceState(dialogState);
        }
    }

    @Override protected void onSaveViewState(@NonNull View view, @NonNull Bundle outState) {
        super.onSaveViewState(view, outState);
        if (dialog != null) {
            Bundle dialogState = dialog.onSaveInstanceState();
            outState.putBundle(SAVED_DIALOG_VIEW_STATE_TAG, dialogState);
        }
    }

This works well for all of view hierarchy, no need to futz about with individial views

MkhytarMkhoian commented 6 years ago

Hi When config change the controller not destroyed and onSaveInstanceState, onRestoreInstanceState not called.

MkhytarMkhoian commented 6 years ago

So, if you use MVP your presenter still alive after config change and you can bind data to UI

ursusursus commented 6 years ago

Okay, but I do think this is a bug

I dont think restoring view focus or position of cursor in edittext should be users responsibility + dialog fragment handles this