2

I have two similar by onViewCreated structure fragments

//1st
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    textDebug1 = getActivity().findViewById(R.id.view_debug1); //from layout1
    textDebug2 = getActivity().findViewById(R.id.view_debug2); //from layout2
//2nd
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    textDebug1 = getActivity().findViewById(R.id.view_debug1); //<--gives  error; layout1
    textDebug2 = getActivity().findViewById(R.id.view_debug2); //from layout2

in onCreateView for both I have same line but different layouts.

     View rootView = inflater.inflate(R.layout.activity_frag_main, container, false);

TextViews are located on different layouts and if I choose to load 1st fragment then code works well, but if I choose 2nd to run 1st then getActivity() returns null. What might cause this if creation is identical for both?

JayJayAbrams
  • 195
  • 1
  • 16

2 Answers2

2

If you look at the Fragment lifecycle, you'll see that there's an onAttach() and an onDetach() methods. They refer to the Fragment's state. Since you're using a ViewPager, your fragments might be detached and the getActivity() might return null.

As stated on this article, this is what happens with a Fragment inside a ViewPager:

When the page is no longer visible or adjacent to the visible page the ViewPager asks the adapter to destroy it. However the FragmentPagerAdapter doesn't destroy the fragment entirely. It calls FragmentTransaction.detach(fragment), which destroys the fragment's entire view hierarchy, but not the object. Next time the ViewPager wants that page the same fragment object can be retrieved and the view is rebuilt. In the process the onCreateView() is called again, this is where your logic to initialise the view belongs.

And as stated on the docs:

Caution: If you need a Context object within your Fragment, you can call getContext(). However, be careful to call getContext() only when the fragment is attached to an activity. When the fragment isn't attached yet, or was detached during the end of its lifecycle, getContext() returns null.

One thing you can try is to retain the fragment instance. Inside your Fragment's onCreate(), try to call the method setRetainInstance(true);

Also, check this answer and this other one here on SO, it might be useful for your use case.

Mauker
  • 11,237
  • 7
  • 58
  • 76
0

Another solution as @Mauker suggested via onAttach()

getActivity() returns null on fragment?

but instead of adding mcontext=getContext(); to each Fragment start added this to own class extention of FragmentPagerAdapter

public class TabAdapter extends FragmentPagerAdapter {

final int PAGE_COUNT = 2;
private Context mContext;

public TabAdapter(Context context, FragmentManager fm) {
    super(fm);
    mContext = context;
}
JayJayAbrams
  • 195
  • 1
  • 16