Jump to content

fragment + tabhost + activity


Recommended Posts

J'ai un écran à onglet (géré par le tabhost) et j'utilise fragmentactivity et fragment (en remplacement de tabactivity).

Je souhaiterais lancer dans le fragment créé une activity (avec layout et gestion d'une base et de bouton).

Est-ce possible? Comment lancer une activity à partir d'une classe fragment?

J'arrive bien à "inflater" le layout attendu mais comment lancer l'activity lié au layout tout en gardant les onglets (tabhost)?

Merci pour votre aide

Le code de ma principale activité :

import java.util.HashMap;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentTransaction;

import android.view.View;

import android.widget.TabHost;

import android.widget.TabHost.TabSpec;

public class MonActivite extends FragmentActivity {

TabHost mTabHost;

private TabSpec tabSpec;

TabManager mTabManager;


public void onCreate(Bundle savedInstanceState) {



mTabHost = (TabHost)findViewById(android.R.id.tabhost);


mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);


Tab1Fragment.class, null);

mTabManager.addTab(mTabHost.newTabSpec("tab2").setIndicator("tab2"), Tab2Fragment.class, null);

if (savedInstanceState != null) {





protected void onSaveInstanceState(Bundle outState) {


outState.putString("tab", mTabHost.getCurrentTabTag());



* This is a helper class that implements a generic mechanism for

* associating fragments with the tabs in a tab host. It relies on a

* trick. Normally a tab host has a simple API for supplying a View or

* Intent that each tab will show. This is not sufficient for switching

* between fragments. So instead we make the content part of the tab host

* 0dp high (it is not shown) and the TabManager supplies its own dummy

* view to show as the tab content. It listens to changes in tabs, and takes

* care of switch to the correct fragment shown in a separate content area

* whenever the selected tab changes.


public static class TabManager implements TabHost.OnTabChangeListener {

private final FragmentActivity mActivity;

private final TabHost mTabHost;

private final int mContainerId;

private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();

TabInfo mLastTab;

static final class TabInfo {

private final String tag;

private final Class<?> clss;

private final Bundle args;

private Fragment fragment;

TabInfo(String _tag, Class<?> _class, Bundle _args) {

tag = _tag;

clss = _class;

args = _args;



static class DummyTabFactory implements TabHost.TabContentFactory {

private final Context mContext;

public DummyTabFactory(Context context) {

mContext = context;


public View createTabContent(String tag) {

View v = new View(mContext);



return v;



public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {

mActivity = activity;

mTabHost = tabHost;

mContainerId = containerId;



public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {

tabSpec.setContent(new DummyTabFactory(mActivity));

String tag = tabSpec.getTag();

TabInfo info = new TabInfo(tag, clss, args);

// Check to see if we already have a fragment for this tab, probably

// from a previously saved state. If so, deactivate it, because our

// initial state is that a tab isn't shown.

info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);

if (info.fragment != null && !info.fragment.isDetached()) {

FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();





mTabs.put(tag, info);



public void onTabChanged(String tabId) {

TabInfo newTab = mTabs.get(tabId);

if (mLastTab != newTab) {

FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();

if (mLastTab != null) {

if (mLastTab.fragment != null) {




if (newTab != null) {

if (newTab.fragment == null) {

newTab.fragment = Fragment.instantiate(mActivity,newTab.clss.getName(), newTab.args);

ft.add(mContainerId, newTab.fragment, newTab.tag);

} else {




mLastTab = newTab;







Le code de mon fragment dans le quel je voudrais faire un intent d'une activity:

public class Tab1Fragment extends Fragment {

public void onAttach(Activity activity){




public void onCreate(Bundle savedInstanceState) {


//Intent monIntent = new Intent(getActivity(),ActiviteSolde.class);



public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

if (container == null) {

return null;


return (LinearLayout)inflater.inflate(R.layout.solde, container, false);


public void onActivityCreated(Bundle savedInstanceState) {




Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...