FragmentStatePageAdapter cache, how do I reference old fragments after
activity recreated? (for ex - landscape mode)
pIn my app I got a ViewPager which is showing 3 fragments in a swipe views
layout. If the activity is recreated (for example, if I go to landscape
mode) I can't get a reference to the fragments anymore./p pThis is my
Adapter /p precodepublic class TabsPagerAdapter extends
FragmentStatePagerAdapter{ public static final String FRAGMENT_MAP =
fragmentMap; private SparseArraylt;MyFragmentgt; myFragmentMap = new
SparseArraylt;MyFragmentgt;(); public TabsPagerAdapter(FragmentManager
fm){ super(fm); } @Override public Fragment getItem(int arg0) { MyFragment
fragment = new MyFragment(); Bundle args = new Bundle(); String nameOfTab
= actionBar.getTabAt(arg0).getText().toString();
ArrayListlt;VideoBookmarkgt; listOfFragment =
myBookmarkManager.getList(nameOfTab);
args.putParcelableArrayList(MyFragment.ARG_LIST, listOfFragment);
fragment.setArguments(args); myFragmentMap.put(arg0, fragment); return
fragment; } @Override //return mytabs.size()??? public int getCount() {
return NUM_OF_TABS; } public void destroyItem (ViewGroup container, int
position, Object object) { super.destroyItem(container, position, object);
myFragmentMap.remove(position); } public Fragment getFragment(int key) {
return myFragmentMap.get(key); } public SparseArraylt;MyFragmentgt;
getFragmentMap(){ return myFragmentMap; } public void
setNewFragmentMap(SparseArraylt;MyFragmentgt; newMap){ myFragmentMap =
newMap; } } /code/pre pI get null pointer exception in this line/p
precode//get current fragment int index = myViewPager.getCurrentItem();
Fragment fragment = myTabsPagerAdapter.getFragment(index); EditText title
= (EditText) fragment.getView().findViewById(R.id.new_title); /code/pre pI
checked, this is happening because the getItem method is not called, so I
guess the adapter reuses old fragments in some way, but how can I access
this fragments cache to get my old fragments and to be able to use the
findViewById method? Thank you!/p
No comments:
Post a Comment