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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:20:37+00:00 2026-05-22T20:20:37+00:00

is it possible to switch between Fragments without re-creating them all the time? If

  • 0

is it possible to switch between Fragments without re-creating them all the time? If so, how?

In the documentation I found an example of how to replace Fragments.

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

But I don’t want to create my Fragments from the scratch every time I need them.

I also found this example of hiding/showing Fragments:

// The content view embeds two fragments; now retrieve them and attach
// their "hide" button.
FragmentManager fm = getFragmentManager();
addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));

But how would I create a fragment with an ID outside an XML file?

I think this might be related to this question, but there isn’t an answer. :/

Thank you very much in advance,
jellyfish

Edit:

That’s how I’m doing it now:

Fragment shown = fragmentManager.findFragmentByTag(shownFragment);

//...

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (shown != null) fragmentTransaction.hide(shown);

//switch statetement for menu selection, just one example:

SettingsFragment set = (SettingsFragment) fragmentManager.findFragmentByTag(SET);
Toast.makeText(this, "Settings:" + set, Toast.LENGTH_LONG).show();
if (set == null)
{
        set = new SettingsFragment();
        fragmentTransaction.add(R.id.framelayout_content, set, SET);
}
else fragmentTransaction.show(set);
shownFragment = SET;
fragmentTransaction.commit();

If I call up the settings, then something else, and then go back to settings, the toast gives me “null” first and “Settings:SettingsFragment{40ef…” second.

However, if I replace fragmentTransaction.add(R.id.framelayout_content, set, SET); with fragmentTransaction.replace(R.id.framelayout_content, set, SET); I keep getting “null”, “null”, “null”… so it doesn’t seem to find the Fragment by tag.

Edit2:

Adding fragmentTransaction.addToBackStack(null); did the trick. 🙂
This saves the whole hiding/memorizing which fragment is shown part so I suppose it’s the most elegant solution for this.

I found this tutorial quite helpful on the topic.

Edit3:

Looking at my code I realized I could get rid of some parts, so I changed it to:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (shown != null) fragmentTransaction.hide(shown);
Settings set = (Settings) fragmentManager.findFragmentByTag(SET);
if (set == null) set = new Settings();

fragmentTransaction.replace(R.id.framelayout_content, set, SET);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

However, this invoked an IllegalStateException: Fragment already added, much the same like here. Is there an easy way to prevent this? Otherwise I think I might switch back to the hide/show bit.

  • 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-22T20:20:38+00:00Added an answer on May 22, 2026 at 8:20 pm

    It could depend on what you are trying to avoid being re-created.

    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);
    

    In your example example when you hit the back button from your newFragment the previous fragment will be shown (you’ll get an onCreateView, onActivityCreated but no onCreate) so that fragment isn’t being re-created as such. As for you newFragment you can still keep it around if you plan to use it again updating any internal state as required in say onCreate or onActivityCreated.

    EDIT:

    If you simply have a menu list with each entry invoking a different fragment in a right pane then adding to the back stack is not what you want. For this you might get away with calling add(...) on each fragment up-front and simply hide/show each fragment as required (I’ve not tested this). Otherwise I would suggest holding a reference to each fragment, call replace(...) on selecting a different menu item ensuring that you don’t add to the back stack.

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

Sidebar

Related Questions

Is it possible to have several command prompts running simultaneously and switch between them,
Hello is possible to switch between DML commands/operations (Insert,Delete,Update) on Trigger Body?, I try
Is it possible to switch between WiFi networks in WM 6 and .NET CF
Is it possible to define multiple templates for a Repeater's ItemTemplate and switch between
I return empty collections vs. null whenever possible. I switch between two methods for
Is that possible to switch an Open Source Project license from GPL to LGPL
Is it possible to switch off optimization of a specific function? A friend of
Possible Duplicate: Emacs, switch to previous window other-window advances me to the next window
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
I want to switch between vertical and horizontal table layout for same data. Although

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.