Android的DialogFragment getActivity()为null(Android

2019-10-19 15:19发布

我想创建在用户登录之前,显示用户一个倒计时的对话框。 超时是从另一个活动设置。

我写了下面的代码:

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();
    }

}

该对话框调用这些行:

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

即使对话框打开后超时设置, getActivity()setInactivityTimeout()null

如何在片段得到正确的资源?

Answer 1:

DialogFragment.show()是异步-所述DialogFragment实际上并不立即显示-它发送到消息队列的末尾。 我建议提供的超时值作为参数,然后将其设置在onCreateDialog()随标题。 例如:

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();
    }
}

然后,你可以用它显示:

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

我还建议作出的对话与沟通的活动,而不是增加对直接依赖关系的接口SessionActivity (这样你可以在任何活动中重用此,只要它实现你的接口)。



文章来源: Android DialogFragment getActivity() is null