-2

I have TabHost FragmentActivity(SherlockFragmentActivity).

How to get the tab (Fragment) View from FragmentActivity? Without the static way, of course.

My FragmentActivity code

 

public class Landing extends SherlockFragmentActivity implements ProgressBarIndeterminateCallback {

    private ActionBar __ab;
    private TabHost __th;
    private TabManager __tm;
    private boolean __isWorking=false;
    public static Utils __utils;

    @SuppressLint("InlinedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState){
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);
        __ab = getSupportActionBar();
        __ab.setDisplayShowTitleEnabled(false);
        __ab.setBackgroundDrawable( new ColorDrawable(Color.parseColor("#5EBDCF")));
        setSupportProgressBarIndeterminateVisibility(false);
        setContentView(R.layout.tab_bottom_layout);
        __th = (TabHost)findViewById(android.R.id.tabhost);
        __th.setup();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            __th.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
            __th.getTabWidget().setDividerPadding(0);
        } else {
            __th.getTabWidget().setDividerDrawable(getResources().getDrawable(R.drawable.tab_divider));
        }
        __tm = new TabManager(this,__th,android.R.id.tabcontent,this);
        __tm.addTab(createTab("signin","Sign In"),SignIn.class,null);
        __tm.addTab(createTab("signup","Sign Up"),SignUp.class,null);
        if (savedInstanceState != null) {
            __th.setCurrentTabByTag(savedInstanceState.getString("current_tab"));
        }
        __utils = new Utils(getApplicationContext());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.landing, menu);
        return (super.onCreateOptionsMenu(menu));
    }

    @Override
    public boolean onPrepareOptionsMenu (final Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_resend_code:
        break;
        case R.id.menu_reset_password:
        break;
        case R.id.menu_about:
            startActivity(new Intent(getApplicationContext(), About.class));
        break;
        }
        return (super.onOptionsItemSelected(item));
    }

    @Override
    protected void onResume(){
        super.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("current_tab", __th.getCurrentTabTag());
    }

    private TabSpec createTab(final String tag,final String title){
        final View tab = LayoutInflater.from(__th.getContext()).inflate(R.layout.landing_tab, null);
        ((TextView)tab.findViewById(R.id.text)).setText(title);
        return __th.newTabSpec(tag).setIndicator(tab);
    }

    public static class TabManager implements TabHost.OnTabChangeListener {

        private final Landing __landing;
        private final FragmentActivity __fa;
        private final TabHost __th;
        private final int __containerID;
        private final HashMap<String, TabInfo> __ti = new HashMap<String, TabInfo>();
        TabInfo __lastTab;

        static final class TabInfo {

            private final String __tag;
            private final Class<?> __class;
            private final Bundle __args;
            private Fragment __fragment;

            TabInfo(String tag, Class<?> clss, Bundle args) {
                __tag = tag;
                __class = clss;
                __args = args;
            }

        }

        static class DummyTabFactory implements TabHost.TabContentFactory {
            private final Context mContext;
            public DummyTabFactory(Context context) {
                mContext = context;
            }

            @Override
            public View createTabContent(String tag) {
                View v = new View(mContext);
                v.setMinimumWidth(0);
                v.setMinimumHeight(0);
                return v;
            }
        }

        public TabManager(FragmentActivity activity, TabHost tabHost, int containerId, Landing landing) {
            __landing = landing;
            __fa = activity;
            __th = tabHost;
            __containerID = containerId;
            __th.setOnTabChangedListener(this);
        }

        public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
            tabSpec.setContent(new DummyTabFactory(__fa));
            String tag = tabSpec.getTag();
            TabInfo info = new TabInfo(tag, clss, args);
            info.__fragment = __fa.getSupportFragmentManager().findFragmentByTag(tag);
            if (info.__fragment != null && !info.__fragment.isDetached()) {
                FragmentTransaction ft = __fa.getSupportFragmentManager().beginTransaction();
                ft.detach(info.__fragment);
                ft.commit();
            }
            __ti.put(tag, info);
            __th.addTab(tabSpec);
        }

        @Override
        public void onTabChanged(String tabId) {
            if(__landing.__isWorking == false){
                SignUp.__isAvatarProvided = false;
                TabInfo newTab = __ti.get(tabId);
                if (__lastTab != newTab) {
                    FragmentTransaction ft = __fa.getSupportFragmentManager().beginTransaction();
                    if (__lastTab != null) {
                        if (__lastTab.__fragment != null) {
                            ft.detach(__lastTab.__fragment);
                        }
                    }
                    if (newTab != null) {
                        if (newTab.__fragment == null) {
                            newTab.__fragment = Fragment.instantiate(__fa,newTab.__class.getName(), newTab.__args);
                            ft.add(__containerID, newTab.__fragment, newTab.__tag);
                        } else {
                            ft.attach(newTab.__fragment);
                        }
                    }
                    __lastTab = newTab;
                    ft.commit();
                    __fa.getSupportFragmentManager().executePendingTransactions();
                }
            }
        }
    }

    @Override
    public void startProgressBarIndeterminate() {
        __isWorking = true;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                __th.getTabWidget().setEnabled(false);
                setSupportProgressBarIndeterminateVisibility(true);
            }
        });     
    }

    @Override
    public void stopProgressBarIndeterminate() {
        __isWorking = false;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                __th.getTabWidget().setEnabled(true);   
                setSupportProgressBarIndeterminateVisibility(false);
            }
        });
    }

}

 

Bias Tegaralaga
  • 2,240
  • 5
  • 21
  • 26

1 Answers1

0

Inside Activity:

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

                Fragment yourFragment = new YourFragment();

                //put fragment to activity
                //YOUR_FRAGMENT_ID is "@+id/" tag for fragment in your_activity.xml
                getSupportFragmentManager().beginTransaction().replace(R.id.YOUR_FRAGMENT_ID, yourFragment).commit();
                getSupportFragmentManager().executePendingTransactions();

               //get reference to fragment 
               Fragment refToYourFragment = (Fragment) getSupportFragmentManager().findFragmentById(YOUR_FRAGMENT_ID);
            }
Rodion Altshuler
  • 1,713
  • 1
  • 15
  • 31
  • I want to get the view, not the fragment itself. – Bias Tegaralaga Mar 21 '13 at 08:31
  • It's possible (afaik, yourFragment.findViewById(...) could help). However, You should not access to fragment views from outside, because in this case fragment won't be an inderpendent reusable component as it supposed to be. If You don't need such fragment characteristic, it's might be easier to inflate another layout instead of fragment to your activity layout. This question is covered here: http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity – Rodion Altshuler Mar 21 '13 at 09:55
  • @RodionAltshuler And if i don't define fragments in XML? Please see my related question: http://stackoverflow.com/questions/24833912/refresh-fragment-ui-from-fragmentactivity – Dr.jacky Jul 19 '14 at 07:36