如何在FragmentTabHost中单击选项卡重新加载TabContent?

时间:2014-01-15 06:44:58

标签: android fragment-tab-host

我正在开发一个Android应用程序,我正在使用FragmentTabHost,我正在为每个选项卡维护一个容器,但是当我重新点击选项卡时,我遇到了重新加载Tabcontent的问题。

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;

import com.eDeftsoft.FragmentsContainer.AboutContainerFragment;
import com.eDeftsoft.FragmentsContainer.BaseContainerFragment;
import com.eDeftsoft.FragmentsContainer.CityContainerFragment;
import com.eDeftsoft.FragmentsContainer.HomeContainerFragment;
import com.eDeftsoft.FragmentsContainer.PhotosContainerFragment;
import com.eDeftsoft.Utility.CommonDialogues;

public class HomeScreen extends FragmentActivity {

    private static final String TAB_1_TAG = "Home";
    private static final String TAB_2_TAG = "Photos";
    private static final String TAB_3_TAG = "City";
    private static final String TAB_4_TAG = "About";
    private FragmentTabHost mTabHost;

    TabWidget tbwidget;
    HomeContainerFragment homeFragment;
    PhotosContainerFragment photosFragment;
    CityContainerFragment cityFragment;
    AboutContainerFragment aboutFragment;

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

        homeFragment = new HomeContainerFragment();
        photosFragment = new PhotosContainerFragment();
        cityFragment = new CityContainerFragment();
        aboutFragment = new AboutContainerFragment();
    }

    private void initView() {
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(setMyCustomIndicator(this, TAB_1_TAG, "Home"),
                HomeContainerFragment.class, null);

        mTabHost.addTab(setMyCustomIndicator(this, TAB_2_TAG, "Photos"),
                PhotosContainerFragment.class, null);

        mTabHost.addTab(setMyCustomIndicator(this, TAB_3_TAG, "City"),
                CityContainerFragment.class, null);

        mTabHost.addTab(setMyCustomIndicator(this, TAB_4_TAG, "About"),
                AboutContainerFragment.class, null);

        setTabHostColors();
        mTabHost.setCurrentTabByTag("TAB_1_TAG");
        mTabHost.setCurrentTab(0);
        mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        mTabHost.getTabWidget().setStripEnabled(false);

        tbwidget = mTabHost.getTabWidget();

/*I had Also used This But getting error when i reclick  on sametabs*/
//      mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
//
//          @Override
//          public void onTabChanged(String tabId) {
//              // TODO Auto-generated method stub
//              if (tabId.equals(TAB_1_TAG)) {
//                  pushFragments(TAB_1_TAG, homeFragment);
//              } else if (tabId.equals(TAB_2_TAG)) {
//                  pushFragments(TAB_1_TAG, photosFragment);
//
//              } else if (tabId.equals(TAB_3_TAG)) {
//                  pushFragments(TAB_1_TAG, cityFragment);
//              } else {
//                  pushFragments(TAB_1_TAG, aboutFragment);
//              }
//
//          }
//      });

    }

    /*
     * insert the fragment into the FrameLayout
     */
//  public void pushFragments(String tag, Fragment class1) {
//
//      FragmentManager manager = getSupportFragmentManager();
//      FragmentTransaction ft = manager.beginTransaction();
//
//      ft.replace(R.id.realtabcontent, class1);
//      ft.commit();
//  }

    public TabSpec setMyCustomIndicator(Context con, String tag,
            String labeltext) {
        TabHost.TabSpec spec = mTabHost.newTabSpec(tag);

        View tabIndicator = LayoutInflater.from(this).inflate(
                R.layout.tab_indicator, null, false);
        ((TextView) tabIndicator.findViewById(R.id.title)).setText(labeltext);
        // ((ImageView) tabIndicator.findViewById(R.id.icon))
        // .setImageResource(resid);

        return spec.setIndicator(tabIndicator);
    }

    @Override
    public void onBackPressed() {
        boolean isPopFragment = false;
        String currentTabTag = mTabHost.getCurrentTabTag();

        FragmentManager fm = getSupportFragmentManager();

        if (currentTabTag.equals(TAB_1_TAG)) {

            for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
                String ide = fm.getBackStackEntryAt(entry).getName();
                Log.i("TAG" + TAB_1_TAG, "Found fragment: " + ide);
            }

            isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
                    .findFragmentByTag(TAB_1_TAG)).popFragment();
        }

        else if (currentTabTag.equals(TAB_2_TAG)) {

            for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
                String ide = fm.getBackStackEntryAt(entry).getName();
                Log.i("TAG" + TAB_2_TAG, "Found fragment: " + ide);
            }

            isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
                    .findFragmentByTag(TAB_2_TAG)).popFragment();
        }

        else if (currentTabTag.equals(TAB_3_TAG)) {

            for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
                String ide = fm.getBackStackEntryAt(entry).getName();
                Log.i("TAG" + TAB_3_TAG, "Found fragment: " + ide);
            }
            isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
                    .findFragmentByTag(TAB_3_TAG)).popFragment();
        }

        else if (currentTabTag.equals(TAB_4_TAG)) {

            for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
                String ide = fm.getBackStackEntryAt(entry).getName();
                Log.i("TAG" + TAB_4_TAG, "Found fragment: " + ide);
            }
            isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
                    .findFragmentByTag(TAB_4_TAG)).popFragment();
        }

        if (!isPopFragment) {

            CommonDialogues.showAlertDialog(HomeScreen.this,
                    "Application Will Exit", "Do you Want to Exit");

        }
    }

    private void setTabHostColors() {

        for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {

            mTabHost.getTabWidget().getChildAt(i)
                    .setBackgroundColor(Color.TRANSPARENT);
            final TextView tv = (TextView) mTabHost.getTabWidget()
                    .getChildAt(i).findViewById(android.R.id.title);

            if (tv == null)
                continue;
            else
                tv.setTextSize(12);
        }

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mTabHost = null;
    }

}

当我进入tab1的内部片段时,例如来自fragmentA - &gt;片段B和来自片段B - &gt; fragmentC(最后我在fragmentC),当我再次选择Tab1时,我想重新加载标签,FragmentA应该是Apper。 任何帮助将不胜感激。我已经浏览了一些教程和代码库但是找不到我的问题的解决方案。 如何在再次单击第一个选项卡时重新加载第一个选项卡的内容。

2 个答案:

答案 0 :(得分:2)

I had asked a question 
When i go in inner fragments of tab1 , like from fragmentA -> Fragment B and From FragmentB -> fragmentC (and finally i am at fragmentC) , When i select Tab1 again i want to reload tabs and FragmentA should Apper.

I searched a lot and come to the conclusion , We can implement OnTabChangeListner and when any tab is clicked again we can reset it.(I was having need to reload tab when it is clicked 2nd time)

    <!--Layout for Tabs -->



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

        <TabHost
            android:id="@android:id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:baselineAligned="false"
                android:orientation="vertical" >

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1" />

                <!-- android:background="@drawable/footer" -->

                <FrameLayout
                    android:id="@+id/tabframeLayout"
                    android:layout_width="fill_parent"
                    android:layout_height="@dimen/tabframe_height"
                    android:layout_marginTop="1dp"
                    android:background="#FBFAFA" >

                    <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="fill_parent"
                        android:layout_height="@dimen/tab_height"
                        android:background="#F2F0F0" >
                    </TabWidget>
                </FrameLayout>
            </LinearLayout>
        </TabHost>

    </RelativeLayout>



    import java.util.ArrayList;
        import java.util.HashMap;
        import android.content.Context;
        import android.content.Intent;
        import android.graphics.Color;
        import android.graphics.Typeface;
        import android.os.Bundle;
        import android.support.v4.app.Fragment;
        import android.support.v4.app.FragmentActivity;
        import android.support.v4.app.FragmentManager;
        import android.support.v4.app.FragmentTransaction;
        import android.view.Gravity;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.ImageView;
        import android.widget.LinearLayout;
        import android.widget.TabHost;
        import android.widget.TabHost.OnTabChangeListener;
        import android.widget.TextView;

        import com.abc.Fragments.FragmentA;
        import com.abc.Fragments.FragmentA1;
        import com.abc.Fragments.FragmentA2;
        import com.abc.Fragments.FragmentA3;
        import com.abc.Utility.CommonDialogues;

        public class MyHomeScreen extends FragmentActivity implements
                OnTabChangeListener {

            private TabHost tabHost;
            private String currentSelectedTab;
            private HashMap<String, ArrayList<Fragment>> hMapTabs;

            final int TEXT_ID = 100;

            final String arrTabLabel[] = { "FragmentA", "FragmentA1", "FragmentA2",
                    "FragmentA3" };

            final static int arrIcons[] = { R.drawable.homee, R.drawable.photoi,
                    R.drawable.cityi, R.drawable.abouti };

            private MyTabView arrTabs[] = new MyTabView[4];

            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.my_home_screen);

                hMapTabs = new HashMap<String, ArrayList<Fragment>>();
                hMapTabs.put(AppConstant.TAB_1_TAG, new ArrayList<Fragment>());
                hMapTabs.put(AppConstant.TAB_2_TAG, new ArrayList<Fragment>());
                hMapTabs.put(AppConstant.TAB_3_TAG, new ArrayList<Fragment>());
                hMapTabs.put(AppConstant.TAB_4_TAG, new ArrayList<Fragment>());

                tabHost = (TabHost) findViewById(android.R.id.tabhost);
                tabHost.setOnTabChangedListener(this);
                tabHost.setup();

                TabHost.TabSpec spec = tabHost.newTabSpec(AppConstant.TAB_1_TAG);
                tabHost.setCurrentTab(0);
                arrTabs[0] = new MyTabView(this, 0, arrTabLabel[0]);
                spec.setContent(new TabHost.TabContentFactory() {
                    public View createTabContent(String tag) {
                        return findViewById(android.R.id.tabcontent);
                    }
                });
                spec.setIndicator(arrTabs[0]);
                tabHost.addTab(spec);

                spec = tabHost.newTabSpec(AppConstant.TAB_2_TAG);
                arrTabs[1] = new MyTabView(this, 1, arrTabLabel[1]);
                spec.setContent(new TabHost.TabContentFactory() {
                    public View createTabContent(String tag) {
                        return findViewById(android.R.id.tabcontent);
                    }
                });

                spec.setIndicator(arrTabs[1]);
                tabHost.addTab(spec);

                spec = tabHost.newTabSpec(AppConstant.TAB_3_TAG);
                arrTabs[2] = new MyTabView(this, 2, arrTabLabel[2]);
                spec.setContent(new TabHost.TabContentFactory() {
                    public View createTabContent(String tag) {
                        return findViewById(android.R.id.tabcontent);
                    }
                });
                spec.setIndicator(arrTabs[2]);
                tabHost.addTab(spec);

                spec = tabHost.newTabSpec(AppConstant.TAB_4_TAG);
                arrTabs[3] = new MyTabView(this, 3, arrTabLabel[3]);
                spec.setContent(new TabHost.TabContentFactory() {
                    public View createTabContent(String tag) {
                        return findViewById(android.R.id.tabcontent);
                    }
                });
                spec.setIndicator(arrTabs[3]);
                tabHost.addTab(spec);

                // set background for Selected Tab
                TextView tv = (TextView) tabHost.getCurrentTabView().findViewById(
                        TEXT_ID);

                // tv.setTextColor(Color.parseColor("#2882C6"));
                View iv = (View) tabHost.getCurrentTabView();
                // iv.setBackgroundResource(R.color.green);

                // Listner for Tab 1//
                tabHost.getTabWidget().getChildAt(0)
                        .setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                if (hMapTabs.size() > 0) {

                                    if (tabHost.getTabWidget().getChildAt(0)
                                            .isSelected()) {
                                        if (hMapTabs.get(AppConstant.TAB_1_TAG).size() > 1) {
                                            resetFragment();

                                        }
                                    }
                                    tabHost.getTabWidget().setCurrentTab(0);
                                    tabHost.setCurrentTab(0);
                                }
                            }
                        });

                /* Listner for Tab 2 */
                tabHost.getTabWidget().getChildAt(1)
                        .setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View arg0) {
                                // TODO Auto-generated method stub
                                if (hMapTabs.size() > 0) {

                                    if (tabHost.getTabWidget().getChildAt(1)
                                            .isSelected()) {
                                        if (hMapTabs.get(AppConstant.TAB_2_TAG).size() > 1) {
                                            resetFragment();
                                        }
                                    }
                                    tabHost.getTabWidget().setCurrentTab(1);
                                    tabHost.setCurrentTab(1);
                                }
                            }
                        });

                /* Listner for Tab 3 */
                tabHost.getTabWidget().getChildAt(2)
                        .setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View arg0) {
                                // TODO Auto-generated method stub
                                if (hMapTabs.size() > 0) {

                                    if (tabHost.getTabWidget().getChildAt(2)
                                            .isSelected()) {
                                        if (hMapTabs.get(AppConstant.TAB_3_TAG).size() > 1) {
                                            resetFragment();
                                        }
                                    }
                                    tabHost.getTabWidget().setCurrentTab(2);
                                    tabHost.setCurrentTab(2);
                                }
                            }
                        });
                /* Listner for Tab 4 */
                tabHost.getTabWidget().getChildAt(3)
                        .setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View arg0) {
                                // TODO Auto-generated method stub
                                if (hMapTabs.size() > 0) {

                                    if (tabHost.getTabWidget().getChildAt(3)
                                            .isSelected()) {
                                        if (hMapTabs.get(AppConstant.TAB_4_TAG).size() > 1) {
                                            resetFragment();
                                        }
                                    }
                                    tabHost.getTabWidget().setCurrentTab(3);
                                    tabHost.setCurrentTab(3);
                                }
                            }
                        });

            }

            /* Method for adding fragment */

            public void addFragments(String tabName, Fragment fragment,
                    boolean animate, boolean add) {
                if (add) {
                    hMapTabs.get(tabName).add(fragment);
                }
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();
                if (animate) {
                    ft.setCustomAnimations(R.animator.slide_in_right,
                            R.animator.slide_out_left);
                }
                ft.replace(android.R.id.tabcontent, fragment);
                ft.commit();
            }

            /* Method for remove fragment */

            public void removeFragment() {
                Fragment fragment = hMapTabs.get(currentSelectedTab).get(
                        hMapTabs.get(currentSelectedTab).size() - 2);
                hMapTabs.get(currentSelectedTab).remove(
                        hMapTabs.get(currentSelectedTab).size() - 1);
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();
                ft.setCustomAnimations(R.animator.slide_in_left,
                        R.animator.slide_out_right);
                ft.replace(android.R.id.tabcontent, fragment);
                ft.commit();
            }

            // reset frgment used when clicked on same tab
            private void resetFragment() {
                Fragment fragment = hMapTabs.get(currentSelectedTab).get(0);
                hMapTabs.get(currentSelectedTab).clear();
                hMapTabs.get(currentSelectedTab).add(fragment);
                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();
                ft.setCustomAnimations(R.animator.slide_in_left,
                        R.animator.slide_out_right);
                ft.replace(android.R.id.tabcontent, fragment);
                ft.commit();

            }

            @Override
            public void onBackPressed() {

                if (hMapTabs.get(currentSelectedTab).size() <= 1) {
                    // super.onBackPressed();

                    CommonDialogues.showAlertDialog(MyHomeScreen.this,
                            "Application Will Exit", "Do you Want to Exit");
                } else {
                    removeFragment();
                }

            }

            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (hMapTabs.get(currentSelectedTab).size() == 0) {
                    return;
                }
                hMapTabs.get(currentSelectedTab)
                        .get(hMapTabs.get(currentSelectedTab).size() - 1)
                        .onActivityResult(requestCode, resultCode, data);
            }

            @Override
            public void onTabChanged(String tabName) {
                // TODO Auto-generated method stub
                currentSelectedTab = tabName;

                // make iteration for unselected tab and make normal background
                for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
                    TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i)
                            .findViewById(TEXT_ID);

                    tv.setTextColor(Color.parseColor("#BDBDBD"));
                    View iv = (View) tabHost.getTabWidget().getChildAt(i);
                    iv.setBackgroundColor(0x00000000);
                }
                TextView tv = (TextView) tabHost.getCurrentTabView().findViewById(
                        TEXT_ID); // for Selected Tab
                tv.setTextColor(Color.parseColor("#2882C6"));

                View iv = (View) tabHost.getCurrentTabView();

                if (hMapTabs.get(tabName).size() == 0) {

                    if (tabName.equals(AppConstant.TAB_1_TAG)) {
                        addFragments(tabName, new FragmentA(), false, true);
                    } else if (tabName.equals(AppConstant.TAB_2_TAG)) {
                        addFragments(tabName, new FragmentA1(), false, true);
                    } else if (tabName.equals(AppConstant.TAB_3_TAG)) {
                        addFragments(tabName, new FragmentA2(), false, true);
                    } else if (tabName.equals(AppConstant.TAB_4_TAG)) {
                        addFragments(tabName, new FragmentA3(), false, true);
                    }
                } else {
                    addFragments(
                            tabName,
                            hMapTabs.get(tabName).get(hMapTabs.get(tabName).size() - 1),
                            false, false);
                }

                switch (tabHost.getCurrentTab()) {
                case 0:

                    // we can also set background color of tabview
                    // iv.setBackgroundResource(R.color.green);
                    break;
                case 1:
                    // iv.setBackgroundResource(R.color.red);
                    break;
                case 2:
                    // iv.setBackgroundResource(R.color.yellow);
                    break;
                case 3:
                    // iv.setBackgroundResource(R.color.twitter);
                    break;

                }

            }

            private class MyTabView extends LinearLayout {
                int nIdx = -1;
                TextView tv;

                public MyTabView(Context c, int drawableIdx, String label) {
                    super(c);
                    ImageView iv = new ImageView(c);
                    nIdx = drawableIdx;
                    // used for forground icons//
                    iv.setImageResource(arrIcons[nIdx]);

                    tv = new TextView(c);
                    tv.setText(label);
                    tv.setGravity(Gravity.BOTTOM);
                    tv.setTextSize(14.0f);
                    tv.setTypeface(null, Typeface.BOLD);
                    tv.setId(TEXT_ID);

                    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, 0.9f);

                    LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, 0.3f);
                    layout.setMargins(0, 3, 0, 0);
                    iv.setLayoutParams(layout);

                    layout.setMargins(0, 3, 0, 2);
                    tv.setLayoutParams(param);
                    tv.setTextColor(Color.parseColor("#BDBDBD"));

                    setOrientation(LinearLayout.VERTICAL);
                    setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                    addView(iv);
                    addView(tv);
                }
            }

        }
    <!--Layout for FragmenA -->

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00ff00"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="TAB 1 FIRST SCREEN"
            android:textColor="@color/dark_blue"
            android:textSize="30sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:text="Go to Next Screen" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName" >
        </EditText>

    </LinearLayout>

    code for fragment A ...it is extend By Basefragment



             import android.os.Bundle;
                import android.view.LayoutInflater;
                import android.view.View;
                import android.view.View.OnClickListener;
                import android.view.ViewGroup;
                import android.widget.Button;
                import android.widget.Toast;

                public class FirstScreen extends BaseFragment implements OnClickListener {
                    private Button btnNext;


                    @Override
                    public void onCreate(Bundle savedInstanceState) {
                        // TODO Auto-generated method stub

                        Toast.makeText(getActivity(), "I am in onCreate", Toast.LENGTH_LONG).show();
                        super.onCreate(savedInstanceState);
                    }

                    @Override
                    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
                        View view = inflater.inflate(R.layout.tab1_firstscreen,
                                container, false);

                        btnNext = (Button) view.findViewById(R.id.btnNext);
                        btnNext.setOnClickListener(this);
                        System.out.println("replace");
                        return view;
                    }



                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub


                        fragmentTabActivity.addFragments(Const.TAB_FIRST,
                                new SecondScreen(), true, true);
                    }



                }



        Code for BaseFragment is 
        extend this class with all sub fragment which you are going to add on (While adding fragment use myhomescreenActivity(This object) and call add Function in MyHomeScreen  ):




             import android.content.Intent;
                import android.os.Bundle;
                import android.support.v4.app.Fragment;

                public class BaseFragment extends Fragment {
                    protected MyHomeScreen myhomescreenActivity;

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

                        myhomescreenActivity = (MyHomeScreen) this.getActivity();
                    }

                    public boolean onBackPressed() {
                        return false;
                    }

                    public void onActivityResult(int requestCode, int resultCode, Intent data) {

                    }
                }


This is the link of repository on Github for further understanding.......
https://github.com/thankimanish/TabUsingFragment

答案 1 :(得分:0)

1.您可以尝试将onCreate方法中的代码移动到onResume方法,以便每次片段到达前面时onResume中的代码都会被执行并重新加载该片段。

2.您也可以尝试重写片段的onHiddenChanged方法,并在片段变为可见时重新加载片段