Note that there are some explanatory texts on larger screens.

plurals
  1. POsupport FragmentPagerAdapter holds reference to old fragments
    primarykey
    data
    text
    <p>LATEST INFO:</p> <p>i have narrowed my problem down to being a problem with the fragmentManager retaining instances of old fragments and my viewpager being out of sync with my FragmentManager. See this issue... <a href="http://code.google.com/p/android/issues/detail?id=19211#makechanges" rel="noreferrer">http://code.google.com/p/android/issues/detail?id=19211#makechanges</a> . I still have no clue how to solve this. Any suggestions...</p> <p>I have tried to debug this for a long time and any help would be greatly appreciated. I am using a FragmentPagerAdapter which accepts a list of fragments like so:</p> <pre><code>List&lt;Fragment&gt; fragments = new Vector&lt;Fragment&gt;(); fragments.add(Fragment.instantiate(this, Fragment1.class.getName())); ... new PagerAdapter(getSupportFragmentManager(), fragments); </code></pre> <p>The implementation is standard. I am using ActionBarSherlock and v4 computability library for Fragments.</p> <p>My problem is that after leaving the app and opening several other applications and coming back, the fragments lose their reference back to the FragmentActivity (ie. <code>getActivity() == null</code>). I can not figure out why this is happening. I tried to manually set <code>setRetainInstance(true);</code> but this does not help. I figured that this happens when my FragmentActivity gets destroyed, however this still happens if I open the app before I get the log message. Are there any ideas?</p> <pre><code>@Override protected void onDestroy(){ Log.w(TAG, "DESTROYDESTROYDESTROYDESTROYDESTROYDESTROYDESTROY"); super.onDestroy(); } </code></pre> <p>The adapter:</p> <pre><code>public class PagerAdapter extends FragmentPagerAdapter { private List&lt;Fragment&gt; fragments; public PagerAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments) { super(fm); this.fragments = fragments; } @Override public Fragment getItem(int position) { return this.fragments.get(position); } @Override public int getCount() { return this.fragments.size(); } } </code></pre> <p>One of my Fragments stripped but i commetned everyhting out thats stripped and still doesnt work...</p> <pre><code>public class MyFragment extends Fragment implements MyFragmentInterface, OnScrollListener { ... @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); handler = new Handler(); setHasOptionsMenu(true); } @Override public void onAttach(Activity activity) { super.onAttach(activity); Log.w(TAG,"ATTACHATTACHATTACHATTACHATTACH"); context = activity; if(context== null){ Log.e("IS NULL", "NULLNULLNULLNULLNULLNULLNULLNULLNULLNULLNULL"); }else{ Log.d("IS NOT NULL", "NOTNOTNOTNOTNOTNOTNOTNOT"); } } @Override public void onActivityCreated(Bundle savedState) { super.onActivityCreated(savedState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.my_fragment,container, false); return v; } @Override public void onResume(){ super.onResume(); } private void callService(){ // do not call another service is already running if(startLoad || !canSet) return; // set flag startLoad = true; canSet = false; // show the bottom spinner addFooter(); Intent intent = new Intent(context, MyService.class); intent.putExtra(MyService.STATUS_RECEIVER, resultReceiver); context.startService(intent); } private ResultReceiver resultReceiver = new ResultReceiver(null) { @Override protected void onReceiveResult(int resultCode, final Bundle resultData) { boolean isSet = false; if(resultData!=null) if(resultData.containsKey(MyService.STATUS_FINISHED_GET)){ if(resultData.getBoolean(MyService.STATUS_FINISHED_GET)){ removeFooter(); startLoad = false; isSet = true; } } switch(resultCode){ case MyService.STATUS_FINISHED: stopSpinning(); break; case SyncService.STATUS_RUNNING: break; case SyncService.STATUS_ERROR: break; } } }; public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { menu.clear(); inflater.inflate(R.menu.activity, menu); } @Override public void onPause(){ super.onPause(); } public void onScroll(AbsListView arg0, int firstVisible, int visibleCount, int totalCount) { boolean loadMore = /* maybe add a padding */ firstVisible + visibleCount &gt;= totalCount; boolean away = firstVisible+ visibleCount &lt;= totalCount - visibleCount; if(away){ // startLoad can now be set again canSet = true; } if(loadMore) } public void onScrollStateChanged(AbsListView arg0, int state) { switch(state){ case OnScrollListener.SCROLL_STATE_FLING: adapter.setLoad(false); lastState = OnScrollListener.SCROLL_STATE_FLING; break; case OnScrollListener.SCROLL_STATE_IDLE: adapter.setLoad(true); if(lastState == SCROLL_STATE_FLING){ // load the images on screen } lastState = OnScrollListener.SCROLL_STATE_IDLE; break; case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: adapter.setLoad(true); if(lastState == SCROLL_STATE_FLING){ // load the images on screen } lastState = OnScrollListener.SCROLL_STATE_TOUCH_SCROLL; break; } } @Override public void onDetach(){ super.onDetach(); if(this.adapter!=null) this.adapter.clearContext(); Log.w(TAG, "DETACHEDDETACHEDDETACHEDDETACHEDDETACHEDDETACHED"); } public void update(final int id, String name) { if(name!=null){ getActivity().getSupportActionBar().setTitle(name); } } </code></pre> <p>}</p> <p>The update method is called when a user interacts with a different fragment and the getActivity is returning null. Here is the method the other fragment is calling...</p> <pre><code>((MyFragment) pagerAdapter.getItem(1)).update(id, name); </code></pre> <p>I believe that when the app is destroyed then created again instead of just starting the app up to the default fragment the app starts up and then viewpager navigates to the last known page. This seems strange, shouldn't the app just load to the default fragment? </p>
    singulars
    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.
 

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