Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Refresh "parent" fragment when DialogFragment is dismissed

I'm building an Android application with a tabbed layout using Tabhost and Fragments.

In one of the tabs (fragment) I have a textview which displays the value of a String variable. This tab also has a button which calls a DialogFragment in which i can edit the value of said string, via an EditText box.

It all works as expected, except for one glitch: once the DialogFragment is dismissed and the tab gets focus again, the textview text doesn't refresh automatically. To make it refresh i need to change tab and get back.

Is there an instruction i can add so that when a DialogFragment is dismissed, its parent activity is reloaded/refreshed?

Thanks in advance.

EDIT: Still looking for a solution, couldnt figure out how to use DialogFragment.isdetached

like image 720
tyb Avatar asked Dec 09 '22 02:12

tyb


2 Answers

To set a keyword, this can be done via the Handler mechanism.

I had a similar situation, displaying showing a DialogFragment from a Fragment, where the DialogFragment should report its' dismiss to the Fragment.

In your case you seem to use a (Fragment)Activity instead of Fragment, but i think that makes no difference here.

Steps

  1. Create a private class within your Activity which extends Handler. Override onHandleMessage(), and refresh your TextViews there.
  2. Pass an instance of YourDialogFragmentDismissHandler to YourDialogFragment, e.g. through a constructor
  3. Override onDismiss() in YourDialogFragment, and send a message, in the simplest way an empty one, through instance of YourDialogFragmentDismissHandler.

Code

public class YourActivity extends Activity {
    ...
    public void someMethod() {
        ...
        YourDialoagFragment yourDialoagFragment = 
            new YourDialoagFragment(new YourDialogFragmentDismissHandler());
        yourDialoagFragment.show();
        ...
    }

    private class YourDialogFragmentDismissHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            // refresh your textview's here
        }   
    }
    ...
}


public class YourDialoagFragment extends DialoagFragment {
    ...
    Handler handler;

    public YourDialoagFragment(Handler handler) {
        this.handler = handler
    }
    ...
    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);

        handler.sendEmptyMessage(0);
    }
    ...
}

I hope this helps!

like image 68
stefanjunker Avatar answered May 15 '23 04:05

stefanjunker


Your problem is that dialogFragment's action affects the data or view of the previous fragment.And you want to refresh the previous fragment's data or view after dialogfragment's dismiss. But the previous dialogfragment's dismiss can't invoke the previous fragment's onCreateView() or onCreate(). In this predicament, you can use Broadcast. When you refresh the previous fragment's data or view's data. You can send broadcast. Once the previous fragment receive the broadcast, the previous fragment can refresh now, not at the dismiss of the dialogfragment. I think this is a better solution especially in multithread.

like image 31
Europa Avatar answered May 15 '23 06:05

Europa