2 a641832648 a641832648 于 2016.09.07 16:14 提问

Fragment横竖屏切换时候接口对象为空

//代码如下:就是点击按钮的时候mListener为空!!!为啥

public class LoginDialog extends DialogFragment implements LoginUI{
private Button toRegist;
private ToRegistListener mLisntener;

@Override
@Nullable
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.login_dialog, null);
    toRegist = (Button) view.findViewById(R.id.toregist);
    toRegist.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i("asd", "onClick lisntener = "+mLisntener);//横竖屏切换后mListener为空
            mLisntener.showRegost();
        }
    });
    return view;
}

@Override
public void disminss() {
    disminss();
}

public interface ToRegistListener{
    void showRegost();
}

@Override
public void setListener(ToRegistListener listener) {
    mLisntener = listener;
    Log.i("asd", "lisntener = "+mLisntener);//横竖屏切换的时候mListener不为空
}
@Override
public void showLoginDialog(Activity activity, ToRegistListener listener) {
    mLisntener = listener;
    show(activity.getFragmentManager(), "LoginDialog");
}

2个回答

guo1988zhen
guo1988zhen   2016.09.07 17:24

横竖屏切换 宿主Activity会被重新初始化;可以从这块查看下

a641832648
a641832648   2016.09.07 17:40

我知道了!!就是在mLisntener.showRegost();的时候传递一个getActivity();的参数!!!就是这样mLisntener.showRegost(getActivity());

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!