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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:10:52+00:00 2026-06-17T07:10:52+00:00

I am starting out and this is my first post. Any help will be

  • 0

I am starting out and this is my first post. Any help will be greatly appreciated!

I have successfully implemented tabs with ABS. I would like to change tabs and reload the fragment activity from a button click in another tab. From searching posts it seems everyone has done this using TabHost. Is there any way to do this without TabHost?

Please see diagram picture of what I would like to do: https://i.stack.imgur.com/jotW6.png

Please see code below of MainActivity:

public class MainActivity extends SherlockFragmentActivity {

private int tabSelected = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


            getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


            getSupportActionBar().addTab(
                    getTab(new TabListener<FragmenOne>(this, FragmenOne.class
                            .getName(), FragmenOne.class), "TabOne"));
            getSupportActionBar().addTab(
                    getTab(new TabListener<FragmenTwo>(this, FragmenTwo.class
                            .getName(), FragmenTwo.class), "TabTwo"));
            getSupportActionBar().addTab(
                    getTab(new TabListener<FragmenThree>(this, FragmenThree.class
                            .getName(), FragmenThree.class), "TabThree"));
}

private Tab getTab(TabListener listener, String title) {
    ActionBar.Tab tab = getSupportActionBar().newTab();
    tab.setTabListener(listener);
    tab.setText(title);
    return tab;
}

public class TabListener<T extends Fragment> implements
ActionBar.TabListener {
    private final SherlockFragmentActivity mActivity;
    private final String mTag;
    private final Class<T> mClass;
    private final Bundle mArgs;
    public Fragment mFragment;

    public TabListener(SherlockFragmentActivity activity, String tag,
            Class<T> clz) {
        this(activity, tag, clz, null);
    }

    public TabListener(SherlockFragmentActivity activity, String tag,
            Class<T> clz, Bundle args) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mArgs = args;

        mFragment = mActivity.getSupportFragmentManager()
                .findFragmentByTag(tag);
        if (mFragment != null && !mFragment.isDetached()) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager()
                    .beginTransaction();
            ft.detach(mFragment);
            ft.commit();
        }


    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {


                        if(tab.getPosition()==0)
                        {
                            tabSelected = 0;
                            FragmenOne frag = new FragmenOne();
                            ft.replace(android.R.id.content, frag);     
                        }
                        else if(tab.getPosition()==1)
                        {
                            tabSelected = 1;
                            FragmenTwo frag = new FragmenTwo();
                            items = 1;
                            invalidateOptionsMenu();
                            ft.replace(android.R.id.content, frag);
                        }
                        else if(tab.getPosition()==2)
                        {
                            tabSelected = 2;
                            FragmenThree frag = new FragmenThree();
                            ft.replace(android.R.id.content, frag);
                        }


    }


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

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }
}

public void setCurrentItem() {

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    FragmenTwo frag = new FragmenTwo();
    ft.replace(android.R.id.content, frag);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.commit();

}

Here is code from the other activity where I would like a button click to load a tab2 for example

public class TabThree extends Activity  implements OnItemSelectedListener {

// Add button
Button btnAdd;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabthree);

    // add button
    btnAdd = (Button) findViewById(R.id.button);



    btnAdd.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {


        //What to write here??????

        }
    });
}
  • 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-17T07:10:54+00:00Added an answer on June 17, 2026 at 7:10 am

    Try this way It is best approach for you should use

    First step : Create InterFace in TabThree Class Like this.

    // The listener we are to notify when a Button is clicked
    OnNotifyButtonClickedListener mOnNotifyButtonClickedListener = null;
    
    /**
     * Represents a listener that will be notified of tab selections.
     */
    public interface OnNotifyButtonClickedListener {
    
        public void OnNotifyButtonClicked();
    }
    /**
     * Sets the listener that should be notified of tab selection events.
     * 
     * @param listener
     *            the listener to notify.
     */
    public void setOnNotifyButtonClickedListener(
            OnNotifyButtonClickedListener listener) {
        mOnNotifyButtonClickedListener = listener;
    }
    

    Second Step : Implements in MainActivity Class Like.

    public class MainActivity extends SherlockFragmentActivity
            implements
            TabThree.OnNotifyButtonClickedListener{
    }
    

    Third step : Click your btnAdd Button in TabThree Class

    btnAdd = (Button) findViewById(R.id.button);
    
    btnAdd.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View arg0) {
    
           mOnNotifyButtonClickedListener.OnNotifyButtonClicked();
    
        }
    });
    

    Fourth step: use Your code & change your tab from TabThree Class

    @Override
        public void OnNotifyButtonClicked() {       
    
        // Put your tab selection code here.
    
        }
    

    & Last main thing Dont forget to Register interface Like

       FragmenThree mFragment= new FragmenThree();
        mFragment.setOnNotifyButtonClickedListener(this);
    

    EDIT : in your case Register this way

       mFragment = mActivity.getSupportFragmentManager()
                    .findFragmentByTag(tag);
            if (mFragment != null && !mFragment.isDetached()) {
    
                mFragment.setOnNotifyButtonClickedListener(this);
                FragmentTransaction ft = mActivity.getSupportFragmentManager()
                        .beginTransaction();
                ft.detach(mFragment);
                ft.commit();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just starting out with shopfiy and this is my first ever stack post! I'm
I am starting to pull my hair out over this! I have been trying
I'm just starting out on my first 'real' cocoa app. Note that this is
I'm new here, and this is my very first post. I have a very
Just starting out, this should be a simple one but I haven't been able
I'm starting out with some XML that looks like this (simplified): <?xml version=1.0 encoding=UTF-8?>
I am just starting out on functions in PostgreSQL, and this is probably pretty
I am just starting out C++, so sorry if this is a dumb question.
Sorry if this is quite noobish to you, but I'm just starting out to
I have been staring at this code and cannot figure out what is wrong

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.