I have a Fragment inside a ViewPager. The Fragment contains a RecyclerView (think ListView). When the user pages away and then returns to the fragment, no matter where the user left the list, the list always restarts at the top. So I need to retain the currPosition
somehow. Would savedInstanceState be a good place to store it? If yes, in which callback do I read the data? Again this is inside a ViewPager and I am using a FragmentPagerAdapter and so not many callbacks are called. Still my broader question stands: What causes a Fragment's savedInstanceState to be non-empty?
To give further breadth to the question. Imagine I have an activity with two fragments. I detach and attach the fragments when needed. Say I navigate away from the activity. Would the savedInstanceState of the fragment be empty in this case if the activity's savedInstanceState is not empty? To be exact here is some code for this second case
private void addMainFragment() {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
Fragment removeFragment = fm.findFragmentByTag(getString(R.string.fragment_a));
if(null != removeFragment){
transaction.detach(removeFragment);
}
Fragment fragment = fm.findFragmentByTag(getString(R.string.fragment_b));
if(null != fragment){
transaction.attach(fragment);
}else{
fragment=MainFragment.newInstance(null,null);
transaction.
add(R.id.fragment_container,fragment,getString(R.string.fragment_b));
}
transaction.commit();
}