Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange bug / behaviours with ViewPager and ActionBar (Sherlock)
    primarykey
    data
    text
    <p>The following bug will happen on an 2.3 device, my setup works fine on 4.x devices. </p> <p>I have a ViewPager with some Fragments in it (they're all of the same class). Every Fragment inflates it's own Menu, because the Menu Items may vary from Fragment to Fragment. </p> <p>For test purposes, I have set up a Menu Item in the ActionBar (the ActionBar is shown on the bottom in the pic because it's a split ActionBar). When the Item is tapped, a TextView in the Fragment should be set to "clicked". This works in the beginning, but after flicking around a bit, this happens: </p> <p>When the Menu Item is tapped, nothing happens. Instead, as soon as I swipe to the next Fragment, the next Fragment sets its TextView to "clicked". It seems like the ActionBar and it's Menu are associated with the next Fragment. </p> <p>Heres a pic </p> <p><img src="https://i.stack.imgur.com/g5C7J.jpg" alt="bug"></p> <p>And heres some code: </p> <p>My Activity: </p> <pre><code>public class MyActivity extends SherlockFragmentActivity implements MyFragment.InvalidateListener { ViewPager viewPager; SectionsPagerAdapter pagerAdapter; public void invalidate() { ActivityCompat.invalidateOptionsMenu(act); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.empty_viewpager); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); viewPager = (ViewPager) findViewById(R.id.pager); viewPager.setAdapter(pagerAdapter); viewPager.setCurrentItem(initialIndex); } public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { Fragment fragment = new MyFragment(); fragment.setHasOptionsMenu(true); return fragment; } // ... } </code></pre> <p>My Fragment: </p> <pre><code>public class MyFragment extends SherlockFragment { HashSet&lt;ImageView&gt; runningImageTasks = new HashSet&lt;ImageView&gt;(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_expose, null); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_grundstueckexpose, menu); // ... } @Override public boolean onOptionsItemSelected(MenuItem mitem) { switch (mitem.getItemId()) { case android.R.id.home: getActivity().finish(); return true; case R.id.myitem: textView.setText("clicked"); return true; default: return super.onOptionsItemSelected(mitem); } } } </code></pre> <p>Has anyone else experienced something like this or has an idea on what could be the problem here?</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.
    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