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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:40:20+00:00 2026-06-13T00:40:20+00:00

I have a problem with a fragmented layout and I sincerely apologize if it

  • 0

I have a problem with a fragmented layout and I sincerely apologize if it has been answered before and I was too dumb to find it. I searched for hours and got nothing (well, I got lots but nothing solved my problem).

So here’s my setup: I have a two pane layout using two FrameLayouts as containers for my fragments. activity_listing.xml:

<FrameLayout android:id="@+id/listing" />
<FrameLayout android:id="@+id/details" />

On opening the app (onCreate in the fragment’s activity) a fragment called Listing is added to FrameLayout “listing” programmatically using a FragmentTransaction.

public class ListingActivity extends FragmentActivity
        implements ListingFragment.Callbacks {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_listing);

    // Displaying the first listing here ...
    Fragment fragment = new ListingFragment();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(R.id.listing, fragment);
    ft.addToBackStack(null);
    ft.commit();
    ...
}

This ListingFragment is replaced a few times during runtime, so I have to create it instead of defining it in XML. I tried the latter with

<fragment android:name="com.example.app.ListingFragment" />

but then it won’t be replaced or removed programmatically later on. I can only add() and then the old one is visible through the new one. But that’s not the problem here – just an explanation for my way.

So far all of this works as it should, Listing is created and displayed etc., so no problems there. But:

When ListingFragment is displayed at startup and I press the back key, at the last position FrameLayout “listing” is emptied instead of dropping back to the Homescreen! I figured it has to be because onCreate of my ListingActivity I display an empty frame and add() a ListingFragment to it. So the back stack has the empty frame in it, too. Right?

I tried solving the situation by this to ListingActivity:

@Override
public void onBackPressed() {
    super.onBackPressed();
    if(getSupportFragmentManager().getBackStackEntryCount() == 0) {
        this.finish();
    }
}

But somehow that does not look or feel right … it looks like a bad work around.

So is there any way to insert the fragment before the view with the empty FrameLayout is inflated so there is no empty state to “back” to? Or is it possible to remove the “empty” state from the back stack even though it is not in it? Any other ideas on how to avoid the empty frame after hitting “back”?

Thank you very much for your efforts 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-06-13T00:40:22+00:00Added an answer on June 13, 2026 at 12:40 am

    Don’t call ft.addToBackStack(null) when you add the Fragment in onCreate. That tells the FragmentManger that you have another state BEFORE that fragment that you want to be able to jump back to.

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

Sidebar

Related Questions

I have been trying to get this layout correct and having trouble. Here is
I have always had problem with the root layout. in the paragraph below, you
I'm using ActionBarSherlock and have a little problem. In the Fragment Tabs example, there
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
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 while loading data into html select when users press or click
I have problem SIMILAR to preventing form data reposting, but not quite the same

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.