tyb
tyb

Reputation: 4749

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

Upvotes: 4

Views: 12050

Answers (6)

hampus millestu
hampus millestu

Reputation: 73

I know this does not answer your question as it's formulated but i don't see why this would't work in your case. Got one more solution if this doesn't work.

In your code for the DialogFragment.

TextView tv = (TextView) getActivity().findViewById(R.id.Your_id); 

tv.setText("new string");

Upvotes: 0

Oguz Ozcan
Oguz Ozcan

Reputation: 1714

Ideal answer is like this: (This is a sample code from my app)

First define an interface in the DialogFragment:

public interface PasswordCreatedCallback{
    void refreshJobsScreen(boolean succesful);
}

And;

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        callback = (PasswordCreatedCallback) getTargetFragment();
    } catch (ClassCastException e) {
        throw new ClassCastException("Calling fragment must implement PasswordCreatedListener interface");
    }
}

And setTargetFragment from the parent fragment like this:

final PasswordCreateDialogFragment passwordDialog = PasswordCreateDialogFragment.newInstance("");
                    passwordDialog.setTargetFragment(ViewPagerFragment.this, Constants.PASSWORD_CREATED);

You can call your callback method anytime you want and your parent fragment will get notified with your callback method.

Upvotes: 0

shmoula
shmoula

Reputation: 1083

I always use simple feedback with onAttach() method (but I really like stefanjunker solution). Just let your activity implement FeedbackListener and implement its methods, then expand your DialogFragment like this:

public class SomeDialog extends DialogFragment {
    private FeedbackListener feedbackListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            feedbackListener = (FeedbackListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement FeedbackListener");
        }
    }

    public void onButtonPushed(View view) {
        feedbackListener.doSomething();
    }

    public interface FeedbackListener {
        public void doSomething();
    }
}

Upvotes: 2

Europa
Europa

Reputation: 147

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.

Upvotes: 2

stefanjunker
stefanjunker

Reputation: 367

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!

Upvotes: 10

Ghouse
Ghouse

Reputation: 516

u can use myDialogFragment.isDetached() which return a boolean value when it is detached called a method where u can write the code for displaying again.

Upvotes: 1

Related Questions