I have a MainActivity
with a ViewPager
. The ViewPager
consists of two Fragments
each holding a single RecyclerView
. I need to have a reference to the RecyclerView
for updating/animating it etc. in the Fragment
.
A problem occurs on screen rotation, because for some reason I cannot set the member variable back to the RecyclerView
.
public abstract class NotifListFragment extends Fragment {
protected RecyclerView mRecyclerView;
public NotifListFragment() {
// Required empty public constructor
EventBusHelper.getBus().register(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mRecyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_notif_list, container, false);
setupRecyclerView(mRecyclerView);
return mRecyclerView;
}
So here in the onCreateView
I always inflate the fragment and save the reference in mRecyclerView
variable. However, when I later try to access it, it is null.
I am using Otto to call both Fragment
's onDataChanged()
method. It is called when a button in the RecyclerView
is pressed.
@Subscribe
public void onDataChanged(DataChangedEvent event) {
List<CustomNotification> oldData = ((NotifRecyclerViewAdapter) mRecyclerView.getAdapter()).getmValues();
List<CustomNotification> newData = getNotifications();
}
The mRecyclerView.getAdapter()
is null in this call.
Fragment's getView()
also returns null. What is happening?