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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:52:49+00:00 2026-06-17T05:52:49+00:00

I’ve got an application with a layout consisting of two fragments. One menu fragment

  • 0

I’ve got an application with a layout consisting of two fragments. One menu fragment to the left and a content fragment to the right. When the user presses a menu item in the menu fragment the content fragment changes.

On a small device (phone) I only display the menu fragment initially. When the user presses a menu item the entire menu fragment is replaced by a content fragment. However, when the user then wants to go back to the menu by pressing the back button, the application closes (as if there is nothing in the back stack).

The following is a trimmed version of my code, what am I doing wrong?

public class MainActivity extends FragmentActivity implements IMenuListener {

    private static final String TAG = MainActivity.class.getName();

    private Fragment menuFragment = null;
    private Fragment contentFragment = null;

    private FragmentManager fm;
    private ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(fm == null) fm = getFragmentManager();
        if(actionBar == null)  actionBar = getActionBar();

        menuFragment = (MenuFragment) fm.findFragmentById(R.id.menuFragment);       
        contentFragment = (Fragment) fm.findFragmentById(R.id.contentFragment);

        if(menuFragment == null) {
            menuFragment = new MenuFragment();
            replaceFragment(R.id.menuFragment, menuFragment);
        }

        // If the content fragment has not been initiated, initiate it, as the view exists in XML.
        if(contentFragment == null && findViewById(R.id.contentFragment) != null) {
            actionBar.setSubtitle(getResources().getString(R.string.actionbar_subtitle_home));
            contentFragment = new HomeFragment();
            replaceFragment(R.id.contentFragment, contentFragment);
        } 
        // If the content fragment has not been initiated and the view does not exist in XML, don't initiate it.
        else if(contentFragment == null && findViewById(R.id.contentFragment) == null) {
             // Basically do nothing.
             actionBar.setSubtitle(getResources().getString(R.string.actionbar_subtitle_menu));
        }
    }

    // Menu item clicked, change fragment
    @Override
    public void onMenuItemSelected(MenuItem item) {
        if(contentFragment != null && contentFragment.isVisible()) {
            // Custom MenuItem class has a Fragment field
            replaceFragment(R.id.contentFragment, item.getFragment()); 
            contentFragment = item.getFragment();
        } else {
            if(deviceIsPhoneInPortaitOrientation()) {
                replaceFragment(R.id.menuFragment, item.getFragment());
            }
        }
    }

    /**
    * Replace current fragment
    * @param container The resource id of the container view
    * @param newFragment The new fragment which should be placed in the given container
    */
    private void replaceFragment(int container, Fragment newFragment) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.addToBackStack(null); // TODO Not working as expected
        ft.replace(container, newFragment);

        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.commit();
    }

    /**
    * Determines whether the current device is a phone with current orientation
    * set to portrait or not.
    * @return true if it is, false otherwise
    */
    public boolean deviceIsPhoneInPortaitOrientation() {
            return (!getResources().getBoolean(R.bool.isTablet) &&
                   getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
    }
}
  • 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-17T05:52:51+00:00Added an answer on June 17, 2026 at 5:52 am

    there is already nice demonstration of exactly what you are look for .

    check the actionBarSherlock fragments sample (or the one provided by google, they are about the same) , in the “layout” demo .

    also check out this tutorial and this lecture

    • 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
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I am using JSon response to parse title,date content and thumbnail images and place
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
i got an object with contents of html markup in it, for example: string
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need to clean up various Word 'smart' characters in user input, including but
I'm making a simple page using Google Maps API 3. My first. One marker

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.