Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:37:24+00:00 2026-06-13T04:37:24+00:00

I’m migrating my dialogs, currently using Activity.showDialog(DIALOG_ID); , to use the DialogFragment system as

  • 0

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

There’s a question that arose during my development when using callbacks to deliver some event back to the activity/fragment that opened the dialog:

Here’s some example code of a simple dialog:

public class DialogTest extends DialogFragment {

public interface DialogTestListener {
    public void onDialogPositiveClick(DialogFragment dialog);
}

// Use this instance of the interface to deliver action events
static DialogTestListener mListener;

public static DialogTest newInstance(Activity activity, int titleId, int messageId) {
    udateListener(activity);
    DialogTest frag = new DialogTest();
    Bundle args = new Bundle();
    args.putInt("titleId", titleId);
    args.putInt("messageId", messageId);
    frag.setArguments(args);
    return frag;
}

public static void udateListener(Activity activity) {
    try {
        // Instantiate the NoticeDialogListener so we can send events with it
        mListener = (DialogTestListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString() + " must implement DialogTestListener");
    }
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int titleId = getArguments().getInt("titleId");
    int messageId = getArguments().getInt("messageId");

    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) {}});
    // dialog positive button
    builder.setPositiveButton("Yes", new OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            mListener.onDialogPositiveClick(DialogTest.this);
        }});

    // create the Dialog object and return it
    return builder.create();
}}

And here’s some activity code calling it:

public class SomeActivity extends FragmentActivity implements DialogTestListener {
private EditText mUserName;
@Override
public void onCreate(Bundle savedInstanceState) {
    // setup ui
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ui_user_edit);
    // name input
    mUserName = (EditText) findViewById(R.id.userEdit_editTextName);
}

@Override
public void onDialogPositiveClick(DialogFragment dialog) {
    Log.d(TAG, this.toString());
    mUserName.setText(mUserName.getText() + "1");
}

private void showDialog() {
    DialogTest test = DialogTest.newInstance(SomeActivity.this, R.string.someTitleText, R.string.someMessageText);
    test.show(getSupportFragmentManager(), "testDialog");
}}

The code is pretty much what you see the reference. Problem is, that once you do a orientation change, when a dialog is shown, it stops working as expected –> Due to the activity lifecycle, both, the activity and the dialog are rebuild, and the dialog now does not have the proper reference to the new rebuilt activity.

I added the following code to my activitys onResume method:

    @Override
protected void onResume() {
    super.onResume();
    DialogTest.udateListener(this);
}

Doing this, I get the expected behavior, and the dialog sends events back to the new rebuilt activity when an orientation change occured.

My question is:
What is the ‘best practice’ to handle the callbacks between the DialogFragment which was opened by a FragmentActivity during an orientation change?

Best regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:37:25+00:00Added an answer on June 13, 2026 at 4:37 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.