0

I use below code to implement tabhost:

public class MyActivity extends FragmentActivity {

    private FragmentTabHost mTabHost = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mylayout);

        mTabHost = (FragmentTabHost) findViewById(R.id.tabHost);
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("A").setIndicator("A"), AFragment.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("B").setIndicator("B"), BFragment.class, null);
    }
}

It contains 2 tabs, AFragment.class as below:

public class AFragment extends Fragment {
    private MyAdapter MyAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_folder_base, container, false);
        ListView ListV = (ListView)view.findViewById(R.id.listview);
        FolderList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              //How to implement to launnch another fragmentC under the same tab
            }
        });

        ...

        MyAdapter = new MyAdapter(this.getActivity(), this.getContext());
        ListV.setAdapter(MyAdapter);
        return view;
    }
}

I want to launch another fragment (FragmentC) when the item has been click, How can I implement it?

And how can I implement onBackPressed() function in these Fragments?

brian
  • 6,802
  • 29
  • 83
  • 124
  • [http://stackoverflow.com/questions/7915413/dynamically-add-fragment-into-fragment](http://stackoverflow.com/questions/7915413/dynamically-add-fragment-into-fragment) – M D Dec 02 '15 at 12:33

1 Answers1

1

A: To add FragmentC in FragmentB, put this in FragmentB:

getActivity().getChildrenFragmentManager()
             .beginTransaction()
             .replace(R.id.fragmentCcontainer, new FragmentC)
             .commit();

where the R.id.fragmentCcontainer is the id of the empty view placed on top of other fragmentB's views/ It is a fragment holder:

fragment_b.xml:

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

    <!--Put FragmentB's Views Here-->

    <FrameLayout
        android:id="@+id/fragmentCcontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

B: There is no onBackPressed() method in fragments. In your case, you should override activity's onBackPressed() method and implement the switch to previous tab in it;

Shayan_Aryan
  • 2,002
  • 1
  • 29
  • 31
  • I try getFragmentManager().beginTransaction().replace(R.id.fragmentCcontainer, new FragmentC).commit(); But the BFragment view still exist and CFragment view will overwrite it. – brian Dec 02 '15 at 13:13
  • try this in fragmentB: getFragmentManager().beginTransaction().replace(((ViewGroup)getView().getParent()).getId(), new FragmentC()).commit() – Shayan_Aryan Dec 02 '15 at 13:31
  • I try this method, it will change view success at the same tab. But if I change tab, the tab1's view still show, and tab2's view overwrite it. – brian Dec 02 '15 at 13:41