显示对话框在片段中显示时的方法

时间:2018-01-08 13:10:04

标签: android android-fragments android-alertdialog

我在片段中打开了对话框,当回到片段时,我的片段视图为null Object
当对话框显示哪个生命周期方法片段正在调用时?

public class MyFragment extends Fragment{
  TextView textView;
  View view;
  FloatingActionButton actionButton;

  @Override
  public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_read, container, false); 

    textView = view.findViewById(R.id.txtSaved);
    actionButton = view.findViewById(R.id.floatingActionButton);

    actionButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        DialogSearch search = new DialogSearch();
        search.show(getActivity().getSupportFragmentManager(),"MyDialog");
      }
    });
   return view;
  }
}

2 个答案:

答案 0 :(得分:0)

做一个小改动,

onPause() of fragment gets called.

您需要返回视图

当对话框显示哪个生命周期方法片段正在调用时?

{{1}}

答案 1 :(得分:0)

问题是对话框是在page2生命周期中显示的,这就是Fragment视图为空的原因。您必须在onCreateView方法中执行此操作。

onViewCreated