All of us have faced this challenge where data of one Fragment got changed from another Fragment but since PageViewer instantiates adjacent Fragments even before Fragment is selected, the changed data does not reflect.
Let us take an example. I have a PageViewer with 3 fragments - One is Search, Second is Cart and 3rd is QuickOrder.
By Default, Search is displayed when this activity starts. Since, Cart is next to Search, it is automatically queued up and instantiated. Now, from Search Fragment, if I add an item to cart, since Cart is already instantiated, even when I select Cart, it does not show the newly added item.
When I select Cart, it automatically instantiates QuickOrder Fragment. Now, no matter what I do, since Cart has been already been instantiated, the new data never shows, even if I move between tabs.
I am sure many of you would have faced this problem. So read a lot and saw suggestions from users at StackOverFlow. This is the working solution that I have.
Solution:
Add a hash map to keep the tags of all the Fragments which have been instantiated. This way, from my FragmentActivity, I can check when the Fragment is the current fragment, in which case I call the onResume() method within the Fragment to refresh the content that I want to refresh.
The video should explain it all.
Source Code:
Inside FragmentPageAdapter which is my FragmentPagerAdapter
I define a hash map to store the Tags of the Fragments
Then we override the instantiateItem method to save the tag of the Fragment in the hashmap along with the position.
Also, we create a method which will return tag of a previously created Fragment based on the position
Inside HomeScreen.java which is my FragmentActivity, inside method on onPageSelected
In myCart.java
Let us take an example. I have a PageViewer with 3 fragments - One is Search, Second is Cart and 3rd is QuickOrder.
By Default, Search is displayed when this activity starts. Since, Cart is next to Search, it is automatically queued up and instantiated. Now, from Search Fragment, if I add an item to cart, since Cart is already instantiated, even when I select Cart, it does not show the newly added item.
When I select Cart, it automatically instantiates QuickOrder Fragment. Now, no matter what I do, since Cart has been already been instantiated, the new data never shows, even if I move between tabs.
I am sure many of you would have faced this problem. So read a lot and saw suggestions from users at StackOverFlow. This is the working solution that I have.
Solution:
Add a hash map to keep the tags of all the Fragments which have been instantiated. This way, from my FragmentActivity, I can check when the Fragment is the current fragment, in which case I call the onResume() method within the Fragment to refresh the content that I want to refresh.
The video should explain it all.
Source Code:
Inside FragmentPageAdapter which is my FragmentPagerAdapter
I define a hash map to store the Tags of the Fragments
mFragmentTags = new HashMap<Integer,String>();
Then we override the instantiateItem method to save the tag of the Fragment in the hashmap along with the position.
@Override public Object instantiateItem(ViewGroup container, int position) { Object obj = super.instantiateItem(container, position); if (obj instanceof Fragment) { // record the fragment tag here. Fragment f = (Fragment) obj; String tag = f.getTag(); mFragmentTags.put(position, tag); } return obj; }
Also, we create a method which will return tag of a previously created Fragment based on the position
public Fragment getFragment(int position) { String tag = mFragmentTags.get(position); if (tag == null) return null; return mFragmentManager.findFragmentByTag(tag); }
Inside HomeScreen.java which is my FragmentActivity, inside method on onPageSelected
Fragment fragment = ((FragmentPageAdapter)viewpager.getAdapter()).getFragment(arg0); if (arg0 ==1 && fragment != null) { fragment.onResume(); }
In myCart.java
@Override public void onResume() { //do the data changes. In this case, I am refreshing the arrayList cart_list and then calling the listview to refresh. getCartData(); lv1.setAdapter(new custom_list_one(this.getActivity(),cart_list)); }
No comments:
Post a Comment