getActivity() returns null in Fragment function

后端 未结 15 1709
别那么骄傲
别那么骄傲 2020-11-22 07:28

I have a fragment (F1) with a public method like this

public void asd() {
    if (getActivity() == null) {
        Log.d(\"yes\",\"it is null\");
    }
}
         


        
15条回答
  •  遇见更好的自我
    2020-11-22 08:18

    The order in which the callbacks are called after commit():

    1. Whatever method you call manually right after commit()
    2. onAttach()
    3. onCreateView()
    4. onActivityCreated()

    I needed to do some work that involved some Views, so onAttach() didn't work for me; it crashed. So I moved part of my code that was setting some params inside a method called right after commit() (1.), then the other part of the code that handled view inside onCreateView() (3.).

提交回复
热议问题