Android DialogFragment getActivity() is null

帅比萌擦擦* 提交于 2019-12-13 02:26:21

问题


I am trying to create a dialog which displays the user a countdown before the user is logged out. The timeout is set from another activity.

I wrote the following code:

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;

public class SessionInactivityDialog extends DialogFragment {

    public void setInactivityTimeout(long timeout) {
        Resources res = getActivity().getResources();
        String text = String.format(res.getString(R.string.iminent_logout_text), (timeout / 1000));
        ((AlertDialog)getDialog()).setMessage(text);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle(R.string.iminent_logout);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                SessionActivity activity = (SessionActivity)getActivity();
                activity.resetTimer();
            }
        });

     return builder.create();
    }

}

The dialog is called with these lines:

private void showIminentLogoutDialog(long timeout) {
    mInactivityDialog.show(getFragmentManager(), TAG);
    mInactivityDialog.setInactivityTimeout(timeout);
}

Even though the timeout is set after the dialog has opened, getActivity() in setInactivityTimeout() is null.

How do I get the resources in the fragment correctly?


回答1:


DialogFragment.show() is asynchronous -- the DialogFragment isn't actually immediately displayed -- it's sent to the end of the message queue. I'd suggest providing the timeout value as an argument, and then set it in onCreateDialog() along with the title. For example:

public class SessionInactivityDialog extends DialogFragment {
    public static final String EXTRA_TIMEOUT = "timeout";

    public static SessionInactivityDialog newInstance(long timeout) {
        Bundle args = new Bundle();
        args.putLong(EXTRA_TIMEOUT, timeout);
        SessionInactivityDialog dialog = new SessionInactivityDialog();
        dialog.setArguments(args);
        return dialog;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final SessionActivity activity = (SessionActivity) getActivity();
        final long timeout = getArguments().getLong(EXTRA_TIMEOUT);
        final String msg = activity.getString(R.string.imminent_logout_text, (timeout / 1000));

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(R.string.imminent_logout);
        builder.setMessage(msg);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                activity.resetTimer();
            }
        });
        return builder.create();
    }
}

Then you can just show it with:

SessionInactivityDialog.newInstance(timeout)
                       .show(getFragmentManager(), TAG);

I'd also suggest making an interface for the dialog to communicate with the activity, rather than adding the direct dependency on SessionActivity (that way you can reuse this in any Activity, as long as it implements your interface).



来源:https://stackoverflow.com/questions/22490687/android-dialogfragment-getactivity-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!