I have an Activity
with ViewPager
. the ViewPager
have a lot of pages, Just like a book. Each fragment has RecyclerViews
with a lot of content. The following is my use case
1 - When I swipe page, RecyclerView must start from the beginning of the list. 2. If I rotate my device, It should read from exact last position of where I left before rotation.
If I don't use any logic, 2nd scenario works perfectly. ie, rotation. But not the first scenario.
If I do some logic like below. Helps to achieve first scenario, But not the second scenario.
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
if (savedInstanceState==null) {
mIsFirstTime = true;
}
}
@Override
public void onResume() {
super.onResume();
try {
getActivity().invalidateOptionsMenu();
if (!mIsFirstTime) {
mListView.scrollToPosition(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
So My question is
How can I determine a fragment restart is due to screen rotation or Viewpager swipe?
I already tried onConfigurationChanged(Configuration newConfig)
. But it doesn't help me. Could you please help me