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 6912741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:07:48+00:00 2026-05-27T09:07:48+00:00

I’m using the compatibility package v4 in my project and I’m having an issue

  • 0

I’m using the compatibility package v4 in my project and I’m having an issue with keeping a Fragment around after it’s removed from view. I have my Activity displaying 2 fragments…a menu frame on the left, content pane on the right. The menu frame has 3 different menus (Fragments) that can be displayed.

This is how I’m replacing the menu Fragment:

public void showMenuFragment( Fragment fragment, String tag ) {

        showFragment( R.id.menu_frame, fragment, tag, false);
        setLastMenuPushed( tag );
    }

protected void showFragment( int resId, Fragment fragment, String tag, boolean addToBackStack ) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if ( fragmentManager.findFragmentByTag( tag ) != null && fragment.isAdded() ) {
            transaction.remove( fragment );
        }

        transaction.replace( resId, fragment, tag ).setTransition( FragmentTransaction.TRANSIT_FRAGMENT_OPEN ).setBreadCrumbShortTitle( tag );

        if ( addToBackStack ) {
            transaction.addToBackStack( tag );
        }

        transaction.commit();
    }

The menu Fragments require data to be loaded before it can be displayed, so I show a loading spinner. Once it’s loaded the first time, I want to not have to ever load it a second time unless the Activity is finished.

When I call showMenuFragment(...) I try to pass in a Fragment using FragmentManager.findFragmentByTag(String tag) but it’s always null, so I have to make a new Fragment every time I want to show a different menu.

My question is, how do I keep these Fragments around after they are replaced by other menus? I want to avoid keeping manual references to the Fragments because when the device is rotated, the instance of my Activity class is destroyed and I would lose those references.

EDIT: To put in much simpler terms, I want the FragmentManager to keep track of previous Fragments without having to add them to the back stack. I don’t want to hit the back button and see the previous menu shown.

  • 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-05-27T09:07:48+00:00Added an answer on May 27, 2026 at 9:07 am

    Just figured it out. Instead of removing Fragments when I add new ones, I just hide them, then show them when I want to display them again. Here’s my modified methods:

    public void showMenuFragment( Fragment fragment, String tag, boolean addToBackStack ) {
    
            showFragment( R.id.menu_frame, fragment, tag, getLastMenuPushed(), addToBackStack );
            setLastMenuPushed( tag );
    }
    
    protected void showFragment( int resId, Fragment fragment, String tag, String lastTag, boolean addToBackStack ) {
    
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
    
            if ( lastTag != null ) {
                Fragment lastFragment = fragmentManager.findFragmentByTag( lastTag );
                if ( lastFragment != null ) {
                    transaction.hide( lastFragment );
                }
            }
    
            if ( fragment.isAdded() ) {
                transaction.show( fragment );
            }
            else {
                transaction.add( resId, fragment, tag ).setBreadCrumbShortTitle( tag );
            }
    
            if ( addToBackStack ) {
                transaction.addToBackStack( tag );
            }
    
            transaction.commit();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.