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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:27:54+00:00 2026-06-10T09:27:54+00:00

I have read several posts here about detach and attach. But it should be

  • 0

I have read several posts here about detach and attach. But it should be fine since I’m running API 15.

I get a double list from the beginning like you can see on this picture:
image of activity

I have the following code:
(MainActivity)

   package dk.metnik.fragmentandactionbar;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import dk.metnik.fragmentandactionbar.fragments.DCIMFragment;
import dk.metnik.fragmentandactionbar.fragments.ESSFragment;
import dk.metnik.fragmentandactionbar.fragments.TeamshareFragment;

public class FragAndBarActivity extends Activity {

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

    private void setupTabs() {
        // setup action bar for tabs
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);

        Tab tab = actionBar
                .newTab()
                .setText("DCIM")
                .setTabListener(
                        new MyTabListener<DCIMFragment>(this, "DCIM",
                                DCIMFragment.class));
        actionBar.addTab(tab);

        tab = actionBar
                .newTab()
                .setText("ESS")
                .setTabListener(
                        new MyTabListener<ESSFragment>(this, "ESS",
                                ESSFragment.class));
        actionBar.addTab(tab);

        tab = actionBar
                .newTab()
                .setText("TeamShare")
                .setTabListener(
                        new MyTabListener<TeamshareFragment>(this, "TeamShare",
                                TeamshareFragment.class));
        actionBar.addTab(tab);
    }

    public static class MyTabListener<T extends Fragment> implements
            TabListener {
        private Fragment mFragment;
        private final Activity mActivity;
        private final String mTag;
        private final Class<T> mClass;

        /**
         * Constructor used each time a new tab is created.
         * 
         * @param activity
         *            The host Activity, used to instantiate the fragment
         * @param tag
         *            The identifier tag for the fragment
         * @param clz
         *            The fragment's Class, used to instantiate the fragment
         */

        public MyTabListener(Activity activity, String tag, Class<T> clz) {
            mActivity = activity;
            mTag = tag;
            mClass = clz;

            mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
            if (mFragment != null) { // && !mFragment.isDetached()) {
                FragmentTransaction ft = mActivity.getFragmentManager()
                        .beginTransaction();
                // ft.detach(mFragment);
                ft.remove(mFragment);
                ft.commit();
            }
        }

        /* The following are each of the ActionBar.TabListener callbacks */

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // Check if the fragment is already initialized
            if (mFragment == null) {
                // If not, instantiate and add it to the activity
                mFragment = Fragment.instantiate(mActivity, mClass.getName());
                ft.add(R.id.listFragment, mFragment, mTag);
            } else {
                ft.attach(mFragment);
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            if (mFragment != null) {
                ft.detach(mFragment);
            }
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
        }
    }
}

And my main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/listFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize"/>
</LinearLayout>  
  • 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-10T09:27:55+00:00Added an answer on June 10, 2026 at 9:27 am

    You’re instantiating the view with a Fragment via your layout.xml:

    <fragment
        android:id="@+id/listFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize"
        class="dk.metnik.fragmentandactionbar.fragments.TeamshareFragment" />
    

    You’re then creating a Tab system where the first tab will be selected, adding another Fragment (with a transparent background I’m guessing) ontop.

    Remove the class="" and change your <fragment> to a <FrameLayout>. That will stop the initial Fragment loading while giving you a defined View to house your Fragment.

    Also, you’re adding your new fragments to the Android base View android.R.id.content. This is reasonable depending on your functionality but if you want the Fragment to take the space of the element you should be using its id. Change:

    ft.add(android.R.id.content, mFragment, mTag); 
    

    to

    ft.add(R.id.listFragment, mFragment, mTag);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read several posts about the configuration manager in VS2010 (or before) but
I've read several posts here about ignoring files in Mercurial but I'm stumped on
All - I have read several posts here about svn repositories layout best practices
I have read several posts on SO about writing and compiling dynamic C# code.
I've read several of the posts on stack overflow already about this topic, but
I have a question about the exit button. I have read several posts on
I read all the posts (here and elsewhere) about the Places API and still
I have read several posts on this topic, but have yet to find a
I've read several posts about java.net vs java.nio here on StackOverflow and on some
I have read and re-read several posts about embedded and linked documents in Mongoose.

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.