Note that there are some explanatory texts on larger screens.

plurals
  1. POAction items from Viewpager initial fragment not being displayed
    primarykey
    data
    text
    <p>In the application I am developing I am using a ViewPager with fragments and each fragment constructs its own menu independently of all of the other fragments in the ViewPager.</p> <p>The issue is that sometimes the fragments that are initialised by the ViewPager by default (i.e in it's initial state) are not having their items populated into the action items menu. What's worse is that this issue only occurs intermittently. If I swipe through the ViewPager enough so that the fragments are forced to re-initialise them selves, when I swipe back, the menu populates correctly.</p> <p>Activity code:</p> <pre><code>package net.solarnz.apps.fragmentsample; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.os.Bundle; import android.support.v13.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; public class FragmentSampleActivity extends Activity { private ViewPagerAdapter mViewPagerAdapter; private ViewPager mViewPager; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if (mViewPagerAdapter == null) { mViewPagerAdapter = new ViewPagerAdapter(getFragmentManager()); } mViewPager = (ViewPager) findViewById(R.id.log_pager); mViewPager.setAdapter(mViewPagerAdapter); mViewPager.setCurrentItem(0); } private class ViewPagerAdapter extends FragmentStatePagerAdapter { public ViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return 8; } @Override public Fragment getItem(int position) { Fragment f = Fragment1.newInstance(position); // f.setRetainInstance(true); f.setHasOptionsMenu(true); return f; } } } </code></pre> <p>Fragment code:</p> <pre><code>package net.solarnz.apps.fragmentsample; import android.app.Fragment; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; public class Fragment1 extends Fragment { int mNum; static Fragment newInstance(int num) { Fragment1 f = new Fragment1(); // Supply num input as an argument. Bundle args = new Bundle(); args.putInt("num", num); f.setArguments(args); return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); mNum = getArguments() != null ? getArguments().getInt("num") : 0; } public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_list, menu); } } </code></pre> <p>Layout:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;android.support.v4.view.ViewPager android:id="@+id/log_pager" android:layout_width="match_parent" android:layout_height="match_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Menu:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;menu xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:id="@+id/menu_refresh" android:title="Refresh" android:icon="@android:drawable/ic_delete" android:showAsAction="ifRoom|withText" /&gt; &lt;/menu&gt; </code></pre> <p>Action menu being populated: <a href="https://i.stack.imgur.com/QFMDd.png" rel="noreferrer">http://i.stack.imgur.com/QFMDd.png</a></p> <p>Action menu not being populated: <a href="https://i.stack.imgur.com/sH5Pp.png" rel="noreferrer">http://i.stack.imgur.com/sH5Pp.png</a></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.
 

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