getActivity() returns null on fragment?

2019-06-14 19:21发布

I have MainActivity() and a FirstFragment().

I'm calling a function which is in FirstFragment from MainActivity().

The problem is, that time getActivity() returns null ?

6条回答
Deceive 欺骗
2楼-- · 2019-06-14 19:47
// declare a variable activity in your fragment
private Activity activity;

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    if (context instanceof Activity) {
        this.activity = (Activity) context;
    }

}

@Override
public void onDetach() {
    super.onDetach();
    this.activity = null;
}


private void initToolbar() {

    // then use the var in any function
    if (activity == null) {
        return;
    }

}
查看更多
Emotional °昔
3楼-- · 2019-06-14 19:53

Instead of using Activity reference. You can create reference of Context class.

private Context context;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof Activity) {//Name of your activity
        this.context = (Activity) context;
    }
}
查看更多
霸刀☆藐视天下
4楼-- · 2019-06-14 19:55

I think you should pass context try like this

if(isNetworkAvailable(getActivity().getContext())) 
{
    System.out.println("Internet is On.");
} 
查看更多
叼着烟拽天下
5楼-- · 2019-06-14 19:59

Do this when fragment start

public void onViewCreated(View view, Bundle savedInstanceState) {
    mcontext=getContext();

mcontext will easily be casted as activity if you need it.

Since Android API level 23, onAttach(Activity activity) has been deprecated. You need to use onAttach(Context context). http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity) so if you want to use onattach you need to do

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    Activity a;
    if (context instanceof Activity){
         a=(Activity) context;
    }
} 

do not do

  if(getActivity()==null){

get activity will return null when you move the app to the background and return. if you use this it will just ignore this part of the code sometimes which will create bugs you won't understand where they come from.

查看更多
不美不萌又怎样
6楼-- · 2019-06-14 20:02
private Context mContext;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
mContext = context;
}

Then in the where you want to call function in 'MainActivity' like this:

((MainActivity)mContext).theFunctionYouWanToCall();

or if the fragment could belong to more than one activities, then check first:

if(MainActivity instanceOf mContext)
  ((MainActivity)mContext).theFunctionYouWanToCall();

Hope this help!

查看更多
我只想做你的唯一
7楼-- · 2019-06-14 20:03

Please use Activity reference from onAttach(). I think this is the best pratice to use instance of Activity from Fragment

 public class FirstFragment extends Fragment {
 private Activity mActivity;
     @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            mActivity = (Activity) context;//use this one .. this is MainActivity instance u can use this as MainActivity mMainActivity = (MainActivity)mActivity;
        }
  }
查看更多
登录 后发表回答