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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:23:24+00:00 2026-05-29T04:23:24+00:00

In the example on using fragments in the Android docs, when the application is

  • 0

In the example on using fragments in the Android docs, when the application is in ‘dualview’ mode, the details fragment is recreated whenever the application needs to show details for a different title. FragmentTransaction.replace() is used to swap out each old details fragment instance with a new one.

Is this recommended practice? Isn’t it wasteful to create a new UI instance when the real intent (no pun intended) is to update what the UI shows, not the UI itself. It seems to me the only reason to create new instances is if one intends to add them to the backstack so the user can retrace steps. Otherwise, is it safe/advisable to update a fragment directly?

In the case of the example, it would mean a method along the lines of DetailsFragment.setShownIndex(). This would be called, passing in the new title index, instead of recreating DetailsFragment.

Suppose we have a version of the example where one activity manages both fragments, but only shows one at a time, swapping each fragment out as needed. Would it be ok for the activity to create an instance of each fragment, retain references to each, and then simply add or remove these two instances from itself as needed?

One possibly sticky consequence of this would be that, when the titles fragment is in resumed state (i.e. in the ‘foreground’), selecting a title will result in a call to DetailsFragment.setShownIndex() at a time when the details fragment is in stopped state.

Good idea? Bad idea?

Thanks in advance.

  • 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-29T04:23:26+00:00Added an answer on May 29, 2026 at 4:23 am

    Like you said, the main reason to create new Fragment instances is for ease of using the back stack. It is also perfectly safe to reuse an existing Fragment (looking it up using either FragmentManager.findFragmentById() or FragmentManager.findFragmentByTag()). Sometimes you’ll need to make good use of the Fragment methods like isVisible(), isRemoving() etc. so you don’t illegally reference UI components when the DetailsFragment is stopped.

    Anyway in your proposed single-pane Activity with 2 fragments, your setShownIndex method could set a private field in DetailsFragment which is loaded in onCreateView or onActivityCreated.

    e.g.,

    DetailsFragment df = getFragmentManager().findFragmentByTag("details");
    if (df != null) {
        df.setShownIndex(getSelectedIndex());
    } else {
        df = DetailsFragment.newInstance(getSelectedIndex());
    }
    fragmentTransaction.replace(R.id.frame, df, "details").commit();
    

    In both cases, whether df is newly created or reused, onCreateView and onActivityCreated will be called when the DetailsFragment gets added to the container.

    But if you want a back stack, I highly recommend just creating new instances, otherwise you’re just implementing your own back stack for the contents of the DetailsFragment.

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

Sidebar

Related Questions

I am using fragments to show images/pages.I have one Activity(Main) which contains all the
It is my first time using android fragment. I am following this tutorial to
Here is a screenshot of my tablet application (not using fragments for now): So
I was able to create a split view using fragments in landscape mode to
Using fragments for the first time. As fragment in itself an activity, going by
I am using android support package v4 and the following layout to show up
For example: using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.Web.Script.Serialization;
Does anyone have an example using the table object in YUI library. More specifically,
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
Let's have an example: using (var someObject = new SomeObject()) { var someOtherObject =

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.