Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8546655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:05:02+00:00 2026-06-11T13:05:02+00:00

I have a problem with FragmentStatePageAdapter (that I use as adapter for a ViewPager)

  • 0

I have a problem with FragmentStatePageAdapter (that I use as adapter for a ViewPager) and the menu items from the action bar.

When I launch the application, everything is fine. If I move the task to background (for example, pressing HOME button) and I start doing stuff until the activity is ended, then when I go back to my application (through the launcher or a notification I create) everything is fine except there are duplicated menu items in the action bar.

An important detail is that the only duplicated items are those that are created in the onCreateOptionsMenu() of each fragment I use in the ViewPager.

If I replaceFragmentStatePageAdapter with FragmentPageAdapter the items are not duplicated anymore, but the fragments are not shown in the ViewPager (getItem() function from the adapter is never called so it does not return any fragment).

Any ideas? A way to avoid FragmentStatePageAdapter to duplicate menu items? Maybe use FragmentPageAdapter but with a modification to show the fragments? A modification in my fragments?

Here are some code snippets from my application…

How menu items are created inside the fragments:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    /* Create play button */
    final MenuItem mPlay = menu.add(R.string.play_all);

    mPlay.setIcon(R.drawable.ic_play_all);
    mPlay.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    mPlay.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            final List<Song> songs = getSongs();

            if (songs.size() == 0) {
                /* Show message */
                Toast.makeText(mContext, R.string.no_song_list, Toast.LENGTH_LONG).show();
            } else {
                /* Play song list */
                try { PlayManager.getService().playList(songs); } catch (Exception e) {}
            }

            return false;
        }
    });

    /* Create menu */
    super.onCreateOptionsMenu(menu, inflater);
}

How fragments are instantiated in the ViewPager adapter

@Override
public Fragment getItem(int position) {
    final Class<?> cls = mTabs.get(position);

    /* No tab */
    if (cls == null)
        return null;

    /* Instantiate fragment */
    final Fragment fragment = Fragment.instantiate(mContext, cls.getName(), null);

    /* Add to list */
    mFragments.put(position, fragment);

    /* Return fragment */
    return fragment;
}

Thanks!

PS: I tried to change the launchMode of the activity to “singleTop” and I also tried to return in getItem() the previosly created fragment (but that’s useless as getItem() is never called when I return to the application, as I said before).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:05:03+00:00Added an answer on June 11, 2026 at 1:05 pm

    I found a solution to my problem a few days after posting my problem. I’m sorry for putting it here so late.

    The following code fixed my problem:

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    
        outState.remove("android:support:fragments");
    }
    

    As simple as writing that piece of code in the activity where I instantiate and use the fragments.

    I hope this helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have problem with load data from Database i use Primefaces (or other use
I have problem with show or hide form in Window Form Application. I start
I have problem with UIWebView delay when the load image from url. In my
I have problem with context menu. I have got: @Override public void onCreateContextMenu(ContextMenu menu,
I have problem while sending messages from android to PC. Messages are send when
I have problem in creating modular analysis architecture for C# application. Aim: It is
I have problem with Razor syntax and jQuery, I'm trying to append some items
Have problem while getting data from Memcached on .NET MVC solution. I have this
i have problem to pass data from view to controller , i have view
I have problem with implementing Rich Text Editor (all that i tried, e.g. TinyMCE).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.