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

  • Home
  • SEARCH
  • 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 8079323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:04:02+00:00 2026-06-05T16:04:02+00:00

I have a linear layout ( layout_container in skeleton.xml) which is inflated with a

  • 0

I have a linear layout (layout_container in skeleton.xml) which is inflated with a ListFragment (menufragment) when the activity is created. Then, when the user performs a click, this layout is replaced with another ListFragment (albumsfragment).

The problem is that when I press the back button, I go back indeed to the menufragment but the list is twice longer because it has been filled up again with the same items. How could I avoid this ?

OnCreate method, inside the Activity:

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.skeleton);

        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();      
        MenuFragment menufragment = new MenuFragment();  
        fragmentTransaction.add(R.id.layout_container, menufragment, "menufragment");      
        fragmentTransaction.commit();

        }

OnClick method, inside the Activity:

  public void OnMenuClick() {

AlbumsFragment albumsfragment = new AlbumsFragment(); 
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.layout_container, albumsfragment, "albumsfragment");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

}

My Fragment class code :

public class MenuFragment extends ListFragment{

ArrayList<MenuItem> m_parts = new ArrayList<MenuItem>();

     @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            MenuAdapter m_adapter;

            m_parts.add(new MenuItem ("item1"));
            m_parts.add(new MenuItem ("item2"));
            m_parts.add(new MenuItem ("item3"));
            m_parts.add(new MenuItem ("item4"));


            m_adapter = new MenuAdapter(getActivity(), R.layout.menu_row, m_parts);
            setListAdapter(m_adapter);

So each time I go back from AlbumFragment to MenuFragment, the list in MenuFragment grows as item1,item2,item3,item4 are added once again.

I’ve try to add the check if (savedInstanceState == null) before the transaction of the MenuFragment but doesn’t change anything.

What should I do to make MenuFragment be popped up from the back Stack without being inflated again with these items ?

Thanks.

  • 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-05T16:04:04+00:00Added an answer on June 5, 2026 at 4:04 pm

    Take a look at the Fragment lifecycle. onActivityCreated() will be called everytime when returning to aFragment. Try, instead, to move your implementation to onCreate() and that should fix your duplication issue.

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

Sidebar

Related Questions

I have a linear layout with this form <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical
I created one linear layout and I have added two relative layout. I wish
I have a main activity with this layout file: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
I have a linear layout that contains 6 images, which span the linear layout
I have a linear layout which contains an imageview and another linear layout which
I have an activity that contains a linear layout and a fragment placeholder. <RelativeLayout
I have a layout defined in an XML file( base_layout.xml ) which may contain
I have a Layout which contains a textview and editText on the top, then
I have a Linear Layout that has a Button and a TextView on it.
I have a linear layout with several buttons in it. The button images are

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.