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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:00:28+00:00 2026-06-17T18:00:28+00:00

I have an Android activity that has two fragments inside, say A and B.

  • 0

I have an Android activity that has two fragments inside, say A and B. I want to click buttons inside A such that B would be switched to C, D, or E.

The layout each refers to a .xml and the all layout have different buttons calling other other fragments or doing other stuffs….

now in main.java:

public void stackAFragment(int page) {

//depending on which button on the list is clicked, the corresponding fragment is replaced
if (npage = 1){
Fragment f = new FragmentB();}

if (npage = 2){
Fragment f = new FragmentC();}

if (npage = 3){
Fragment f = new FragmentD();}

FragmentTransaction ft = getFragmentManager().beginTransaction()
ft.replace(R.id.frag_content, f); 
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
ft.addToBackStack(null); 
ft.commit();}

then in A.java, there is a list view, and for the listener

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Main main = (Main) getActivity();
main.stackAFragment(arg2 + 1); }

and in B.java:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
View c = inflater.inflate(R.layout.frag_b, container, false);
return c;}

and in C.java:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
View c = inflater.inflate(R.layout.frag_c, container, false);
return c;}

and so on…..

So now, in the first page, it shows fragment A and B.

but when I clicked to change to fragment C, fragment B does not go away, instead, fragment C appears under fragment B

and when I clicked to change to fragment D, fragment C goes away but B still stays….

How could I solve my problem??

Thank very much!!! Highly appreciate if anybody could point me the right way…..

Additional information::::
the layout of main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frags"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SlidingMenu" >

<fragment
    android:id="@+id/frag_content"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    class="com.seapointcorp.enque01.ContentFragment" />

<fragment
    android:id="@+id/frag_title"
    android:layout_width="@dimen/titles_size"
    android:layout_height="match_parent"
    class="com.seapointcorp.enque01.TitleFragment" />

</FrameLayout>
  • 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-17T18:00:30+00:00Added an answer on June 17, 2026 at 6:00 pm

    The main problem with your code is that you’re doing a replace FragmentTransaction using the id of a Fragment item from the layout file(so you basically make the replace in the fragment with id frag_content(which I assume is fragment B)). Instead you should have a wrapper layout(a simple FrameLayout, RelativeLayout etc) to act as an area to which the content fragment will be added/removed/replaced:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frags"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SlidingMenu" >
    
        <FrameLayout android:id="@+id/content"     android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
            <fragment
               android:id="@+id/frag_content"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               class="com.seapointcorp.enque01.ContentFragment" />
        </FrameLayout>
        <fragment
            android:id="@+id/frag_title"
            android:layout_width="@dimen/titles_size"
            android:layout_height="match_parent"
            class="com.seapointcorp.enque01.TitleFragment" />
    
        </FrameLayout>
    </FrameLayout>
    

    Then in your stackAFragment() method you’ll do:

    FragmentTransaction ft = getFragmentManager().beginTransaction()
    ft.replace(R.id.content, new tagetFragment()); 
     ft.addToBackStack(null);
    
                // Commit the transaction
                ft.commit();
    //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Android application that contains two Activities . Activity A has a
I have two fragments in an activity: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=horizontal android:id=@+id/mainlayout android:layout_width=fill_parent android:layout_height=fill_parent> <fragment
I have an Android widget that has a configure activity. I also have an
I have an activity that has two EditText boxes, one email and one plain
I have an android activity that downloads content from the web. The activity have
So I have a very simple android activity that starts a timer when you
I have an android application that launches a child Activity under certain situations. Both
I have a activity that I want to rotate. I use the following in
I'm creating android app that has table layout for the main activity, and that
I have a View in Android that has an onTouchEvent which stores the x

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.