sarath
sarath

Reputation: 3201

android fragment switching

Now I am doing an application using fragments. So i care a fragment activity with four fragments fragment A,fragment B, fragment C, Fragment D. By default Fragment A will be selected and on tab click I can switch to fragment B,C,D or A.

My problem is I want to check a condition in fragment A and depends on result switch to fragment B.Like.

Intent in=new Intent(src,destination);
startactivity(in)

I want to switch to other fragment without clicking tab button.Following is the code i used to add fragment.I want to check a condition depends on that condition fragment a,fragment b tab will selected

 ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab tab1 = bar.newTab();
    ActionBar.Tab tab2 = bar.newTab();
    tab1.setText("Fragment A");
    tab2.setText("Fragment B");
    tab1.setTabListener(new MyTabListener());
    tab2.setTabListener(new MyTabListener());
    bar.addTab(tab1);
    bar.addTab(tab2);

please help me friends.

Upvotes: 3

Views: 1716

Answers (2)

vikki
vikki

Reputation: 2814

If by selecting a tab you can switch to a certain fragment e.g tab1 brings up Fragment A, then all you need to do is select that tab. For example your currently viewing Fragment B and need to check some condition which will determine whether you switch to Fragment A or C

ActionBar bar = getSupportActionBar();
if(condition){
    bar.setSelectedNavigationItem(1); //where 1 is the position of tab1
}else{
    bar.setSelectedNavigationItem(3); //for tab3
}

Upvotes: 2

hackp0int
hackp0int

Reputation: 4161

Fragments are fairly difficult to use, so you should learn about them. Here is how i do it.

FragmentManager:

public class FragmentsUtils {
private static String DIALOG_TAG = "dialog_tag";

/*public static MyAlertDialogFragment showDialog(FragmentManager fragmentManager, int id) {
    // DialogFragment.show() will take care of adding the fragment
    // in a transaction.  We also want to remove any currently showing
    // dialog, so make our own transaction and take care of that here.
    FragmentTransaction ft = fragmentManager.beginTransaction();
    Fragment prev = fragmentManager.findFragmentByTag(DIALOG_TAG);
    if (prev != null) {
        ft.remove(prev);
    }

    //      ft.addToBackStack(null);

    // Create and show the dialog.
    MyAlertDialogFragment newFragment = MyAlertDialogFragment.newInstance(id);
    newFragment.show(ft, DIALOG_TAG);
    return newFragment;
}*/

public static void swapFragments(FragmentManager fragmentManager, int containerViewId, Fragment newFragment, String fragmentTag)    {

    if (fragmentManager == null)
        return;
    View newFragmentMainView = newFragment.getView();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    //      fragmentTransaction.replace(containerViewId, fragment);

    if(fragmentTag == null || fragmentTag.length() < 1)
        fragmentTransaction.add(containerViewId, newFragment);
    else
        fragmentTransaction.add(containerViewId, newFragment, fragmentTag);

    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    fragmentTransaction.commit();
}

public static void swapFragments(FragmentManager fragmentManager, int containerViewId, Fragment newFragment)    {
    swapFragments(fragmentManager, containerViewId, newFragment, null);
}

}

here is how to use it:

     WebViewDetailsActivity webViewDetailsActivity = new WebViewDetailsActivity();
     Bundle args = new Bundle();
     args.putString("url", tempRestModel.ClickTableUrl);   webViewDetailsActivity.setArguments(args); 

FragmentsUtils.swapFragments(getBaseFragmentManager(), R.id.realtabcontent, webViewDetailsActivity, Consts.TAB_REC);

Upvotes: 2

Related Questions