0

I am pretty new to Android development and need help!.

I have implemented Navigation Tabs using ActionBar with the help of tutorial: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs

My application has 3 fragments. I have created a button on fragment 1 and on on click of that button I want to call fragment 2. Can any one tell me how can I accomplish it ?

MenuActivity.java

public class MenuActivity extends Activity {
// Declare Tab Variable
ActionBar.Tab TabPreOp, TabConnect, TabInProcedure, TabPostOp;
Fragment preOpFragmentTab = new PreOpFragmentTab();
Fragment cnCFragmentTab = new CnCFragmentTab();
Fragment inProcedureFragmentTab = new InProcedureFragmentTab();
Fragment postOpFragmentTab = new PostOpFragmentTab();

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

    ActionBar actionBar = getActionBar();

    // Hide Actionbar Icon
    actionBar.setDisplayShowHomeEnabled(false);

    // Hide Actionbar Title
    actionBar.setDisplayShowTitleEnabled(false);

    // Create Actionbar Tabs
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    // Set Tab Icon and Titles
    TabPreOp = actionBar.newTab().setText("Pre-Op");
    TabConnect = actionBar.newTab().setText("Connect & Calibrate");
    TabInProcedure = actionBar.newTab().setText("In-Procedure");
    TabPostOp = actionBar.newTab().setText("Post-Op");



    // Set Tab Listeners
    TabPreOp.setTabListener(new TabListener(preOpFragmentTab));
    TabConnect.setTabListener(new TabListener(cnCFragmentTab));
    TabInProcedure.setTabListener(new TabListener(inProcedureFragmentTab));
    TabPostOp.setTabListener(new TabListener(postOpFragmentTab));

    // Add tabs to actionbar
    actionBar.addTab(TabPreOp);
    actionBar.addTab(TabConnect);
    actionBar.addTab(TabInProcedure);
    actionBar.addTab(TabPostOp);
}

Fragment1

public class PreOpFragmentTab extends Fragment {

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

    Button button_continue = (Button) rootView.findViewById(R.id.button_continue);
    button_continue.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {


        }
    });
    return rootView;
}

}

and I have a TabListner class.

Can someone give me an advice how to do it? Thanks in advance.

Sushant

1 Answers1

0

here is an example for fragment trasactions:

button_continue.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
    Transaction transaction = getSupportFragmentManager().beginTransaction();
    ExampleFragment fragment = new ExampleFragment();
    transaction.replace(R.id.container, fragment); //This R.id.container should be replaced for the id you have in your activity(If u want show me your xml of your parent Activity)
    transaction.addToBackStack(null); //If you want to add to the stack add this line
    transaction.commit();

    }
});

For more information you can see this guide:

http://developer.android.com/guide/components/fragments.html

JpCrow
  • 4,881
  • 4
  • 32
  • 46