1

In my activity, I call a method which is inside of a fragment to update a ListView in the fragment. However, the getActivity() method returns null, the listView is null, the adapter is null too. But I can see the listView is being displayed. Why are they null?

Note: I call FragmentPagerAdapter.getItem(position) to get the fragment. However, it will return a different instance. According to here, I can reuse the fragment by findFragmentByTag.

Community
  • 1
  • 1
ZhangLei
  • 393
  • 1
  • 7
  • 17

1 Answers1

0

It depends when you try to access your activity and listview. In Fragment's constructor all of these are not available since they are not built yet.

  • Activity is available after onActivityCreated() callback is triggered
  • ListView is available after onViewCreated() callback is triggered
Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83