Oleh Toder
Oleh Toder

Reputation: 422

Android DialogFragment lifecycle when it is actually fully visible to user

I have a custom layout with custom Views (class CaseView). Every CaseView is backed by a data object (class Case) (one Case object can have multiple CaseViews drawing it). Case objects have state variable, that can be set, and CaseViews (that draw THIS object) draw themselves differently based on this state variable.
When my CaseView is clicked, I open a DialogFragment with another CaseView in dialog layout, backed by the same Case object.
The problem is, I want to change my opened Case state. (In my case, that will just change CaseView color from gray to yellow). That will also change the color of clicked CaseView in my initial layout (they are both backed by the same object).
Here's the question: what method of my DialogFragment (or any other approach) should I actually use to change Case's state variable in, if I want CaseView color to be changed only when user fully sees my Dialog on screen?
I tried overriding onStart and onResume of DialogFragment to change state, though in both cases the pressed CaseView in my inital Layout changes color BEFORE I can even see my DialogFragment on screen.
Please see the picture: the CaseView in layout (poined by green arrow) becomes yellow before I can actually see my DialogFragment on screen.

That's how I override my onResume method of DialogFragment:

    @Override
    public void onResume() {
        super.onResume();
        if (mCase.getCurrentState() != BankManager.STATE_OPENED) {
            mCase.setCurrentState(BankManager.STATE_OPENED); //STATE_OPENED mean drawing yellow
        }
    }

Case#setCurrentState(int) will call View#Invalidate() on all CaseView (that he is aware of) that are drawing this Case object.

A CasView in layout (pointed by green arrow) becomes yellow before I can see DialogFragment

Upvotes: 0

Views: 803

Answers (1)

Oleh Toder
Oleh Toder

Reputation: 422

Seems that I found a way out.
Added DialogInterface.OnShowListener to my Dialog, and doing my work there.

    //in DialogFragment#onCreateDialog

    //...
    Dialog dialog = builder.create();

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            openShowedCase(); //doing all my state changing there
        }
    });

    return dialog;

Still when I see my changes on in-layout CaseView my DialogFragment is doing some animation popping (I suppose that is some FragmentTransaction custom animation), though it is much better.

Upvotes: 1

Related Questions