Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove all fragments from ViewPager populated by FragmentStatePagerAdapter
    primarykey
    data
    text
    <p>I have a ViewPager that I am populating with fragments(representing objects from arrayListOfObjects) using FragmentStatePagerAdapter. All works well:</p> <pre><code> mMyFragmentPagerAdapter = new fragmentAdapter(getSupportFragmentManager(),orientation ,rePopulatedfireInfoList); mPager = (ViewPager)findViewById(R.id.fireInfoFragment_container); initButton(); setTab(); mPager.setAdapter(mMyFragmentPagerAdapter); </code></pre> <p>The fragment adapter extends FragmentStatePagerAdapter.</p> <p>From the primary activity I launch a dialog themed activity; where the user may add a new favourite location creating a new object which alters the arraylist of objects passed by the primary activity. This is the code for starting dialog activity; all works fine:</p> <pre><code> Intent locationIntent = new Intent(afisController.this, locationActivity.class); locationIntent.putExtra("firesList", new fireInfoListWrapper(arrayListOfObjects)); startActivityForResult(locationIntent,1); </code></pre> <p>The floating activity adds objects into arrayListOfObjects.</p> <p>On the primary activity's onActivityResult I compare the arraListOfObjects I'm receiving with the one I sent; if different I want to completely remove the contents of the viewPager and recreate it with the new arrayListOfObjects. This is the onActivityResults:</p> <pre><code> protected void onActivityResult(int requestCode, int resultCode, Intent data) { Toast.makeText(this, "Activity Results fired..." , 1500 ).show(); if ((resultCode == 0) &amp;&amp; (data != null)) { Log.i("onActvityResult", "Inside resultCode check"); Bundle b = data.getExtras(); if(b != null){ Log.i("onActvityResult", "not null"); returnedFireInfoList = (ArrayList&lt;fireInfo&gt;) data.getSerializableExtra("firesListResult"); Log.i("onActvityResult", "results Size: "+returnedFireInfoList.size()); if(returnedFireInfoList.size()&gt;0){ Log.i("onActvityResult", "locationName: "+returnedFireInfoList.get(0).getLocationName()); //compare returnedFireInfoList and rePopulatedfireInfoList, if different; //add difference to rePopulatedfireInfoList and write back to file. updateFireInfos(returnedFireInfoList, rePopulatedfireInfoList); if(returnedFireInfoList.size()!=rePopulatedfireInfoList.size()){ mMyFragmentPagerAdapter1 = new fragmentAdapter(getSupportFragmentManager(),orientation ,returnedFireInfoList); mPager = (ViewPager)findViewById(R.id.fireInfoFragment_container); Log.i("updateFireInfos", "fragmentsCount is"+mPager.getCurrentItem()); fireInfoFragment fragment = (fireInfoFragment) getSupportFragmentManager().findFragmentById(R.id.fireInfoFragment_container); //This is where the problem is, I don't want to remember what was already on the viewPager //called mPager before. // mPager.removeAllViews(); //mPager.setAdapter(null); mMyFragmentPagerAdapter1.notifyDataSetChanged(); mPager.setAdapter(mMyFragmentPagerAdapter1); mMyFragmentPagerAdapter1.notifyDataSetChanged(); } } } </code></pre> <p>This is the fragmentStateAdapter code:</p> <pre><code>public class fragmentAdapter extends FragmentStatePagerAdapter { private FragmentManager fragmentManager; private FragmentTransaction mCurTransaction = null; private ArrayList&lt;Fragment.SavedState&gt; mSavedState = new ArrayList&lt;Fragment.SavedState&gt;(); private ArrayList&lt;Fragment&gt; mFragments = new ArrayList&lt;Fragment&gt;(); private Fragment mCurrentPrimaryItem = null; public void restoreState(Parcelable state, ClassLoader loader) { //Need to only delete info from marked fragments (theoned that are stored on orientationchange //Currently redoing the entire call; resulting in delay due to server call //if(isLastOrientationPortrait != isPortrait){ if(state != null){ Bundle bundle1 = (Bundle) state; bundle1.setClassLoader(loader); Iterable&lt;String&gt; keys = bundle1.keySet(); Log.i("restoreState", "containsKey FragmentStatePagerAdapter: "+keys); //android.support.v4.app.FragmentManager fragmentManager= fragmentAdapter.this.fragmentManager; android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); //if (fragmentTransaction == null) { // Log.i("restoreState", "fragmentTransactionTest"); fragmentTransaction = fragmentManager.beginTransaction(); // } for (String key : keys) { if (key.startsWith("f")) { Fragment f = fragmentManager.getFragment(bundle1, key); fragmentTransaction.remove(f); fragmentTransaction.commit(); } } } //} } @Override public int getItemPosition(Object object) { // TODO Auto-generated method stub //return super.getItemPosition(object); return fragmentAdapter.POSITION_NONE; } public fragmentAdapter(android.support.v4.app.FragmentManager fragmentManager,String orientation, ArrayList&lt;fireInfo&gt; fireInfoList) { super(fragmentManager); this.orientation = orientation; this.fireInfoList = fireInfoList; this.numItems = fireInfoList.size(); this.fragmentManager=fragmentManager; } ArrayList&lt;fireInfo&gt; fireInfoList; String orientation; int numItems; @Override public int getCount() { Log.i("numItems", "is: "+fireInfoList.size()); return numItems; } @Override public Fragment getItem(int arg0) { Log.i("fragmentAdapterIndex", "is: "+arg0); return fireInfoFragment.newInstance(orientation, fireInfoList.get(arg0)); } </code></pre> <p>}</p> <p><strong>Problem:</strong> But the new ArrayListOfObjects is added alongside the old one before I fired the startActivityFor results.</p> <p>How do I force the viewPager to forget it old content? basically reset the viewPager adapter with this newArrayListofObjects using my fragmentStateAdapter?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload