Example usage for android.support.v4.app FragmentManager beginTransaction

List of usage examples for android.support.v4.app FragmentManager beginTransaction

Introduction

In this page you can find the example usage for android.support.v4.app FragmentManager beginTransaction.

Prototype

public abstract FragmentTransaction beginTransaction();

Source Link

Document

Start a series of edit operations on the Fragments associated with this FragmentManager.

Usage

From source file:com.battlelancer.seriesguide.ui.dialogs.ListManageDialogFragment.java

/**
 * Display a dialog which allows to edit the title of this list or remove
 * it.//from w w  w.j av  a2 s. c o m
 */
public static void showListManageDialog(String listId, FragmentManager fm) {
    // 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 = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag("listmanagedialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = ListManageDialogFragment.newInstance(listId);
    newFragment.show(ft, "listmanagedialog");
}

From source file:Main.java

public static <T extends Fragment> T addFragmentToActivity(@NonNull FragmentManager fragmentManager,
        @NonNull Class<T> clz, @NonNull Integer frameId) {
    checkNotNull(fragmentManager);//  ww  w  .  j  a  v a  2  s. co  m
    checkNotNull(clz);
    checkNotNull(frameId);
    T fragment = (T) fragmentManager.findFragmentById(frameId);
    if (fragment == null) {
        try {
            fragment = clz.newInstance();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.add(frameId, fragment);
            transaction.commit();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    return fragment;
}

From source file:com.aleiacampo.oristanobus.util.NavigationDrawerUtil.java

public static void setFavourites(final AppCompatActivity appCompatActivity) {

    final Bundle bundle = new Bundle();
    final ArrayList<Stop> stopsList;

    SQLiteHelper db = new SQLiteHelper(appCompatActivity);
    stopsList = db.getAllStops();//from  w w  w .j ava 2s  .  co m
    ArrayList<String> stopsNameList = new ArrayList<>();

    for (Stop stop : stopsList) {
        stopsNameList.add(stop.nameStop);
    }

    if (stopsList.isEmpty())
        stopsNameList.add("Nessuna fermata salvata");

    ListView favourites = (ListView) appCompatActivity.findViewById(R.id.favourite_list);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(appCompatActivity.getApplicationContext(),
            R.layout.text_view, stopsNameList);
    favourites.setAdapter(adapter);

    favourites.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            bundle.putInt("id_stop", stopsList.get(position).idStop);
            bundle.putInt("id_line", stopsList.get(position).idLine);
            bundle.putString("name_stop", stopsList.get(position).nameStop);
            bundle.putString("name_line", stopsList.get(position).nameLine);

            DrawerLayout drawer = (DrawerLayout) appCompatActivity.findViewById(R.id.drawer_left);
            drawer.closeDrawer(Gravity.LEFT);

            FragmentManager fragmentManager = appCompatActivity.getSupportFragmentManager();
            TimesFragment timesFragment = new TimesFragment();
            timesFragment.setArguments(bundle);
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.home_frag, timesFragment, "Times");
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

    favourites.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View view, final int pos, long id) {

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(appCompatActivity);
            alertDialog.setMessage("Rimuovere la fermata dai preferiti?");
            alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    SQLiteHelper db2 = new SQLiteHelper(appCompatActivity);
                    db2.deleteStop(stopsList.get(pos).id); // l'id della fermata caricato dal db
                    NavigationDrawerUtil.setFavourites(appCompatActivity);
                    dialog.cancel();
                }
            });
            alertDialog.setNegativeButton("Nope !", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            alertDialog.show();
            return true;
        }
    });

    favourites.setLongClickable(true);
}

From source file:com.cylan.jiafeigou.utils.ActivityUtils.java

public static boolean addFragmentSlideInFromRight(FragmentManager fragmentManager, Fragment fragment,
        int containerId) {
    final String tag = fragment.getClass().getSimpleName();
    Fragment f = fragmentManager.findFragmentByTag(tag);
    if (f != null && f.isVisible()) {
        return false;
    }//  w ww. ja v  a  2 s .  co m
    fragmentManager.beginTransaction()
            .setCustomAnimations(R.anim.slide_right_in, R.anim.slide_out_left, R.anim.slide_out_right,
                    R.anim.slide_out_right)
            .add(containerId, fragment, tag).addToBackStack(tag).commitAllowingStateLoss();
    return true;
}

From source file:com.cylan.jiafeigou.utils.ActivityUtils.java

public static boolean addFragmentSlideInFromLeft(FragmentManager fragmentManager, Fragment fragment,
        int containerId) {
    final String tag = fragment.getClass().getSimpleName();
    Fragment f = fragmentManager.findFragmentByTag(tag);
    if (f != null && f.isVisible()) {
        return false;
    }/*from www . ja v a2s.  co  m*/
    fragmentManager
            .beginTransaction().setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left,
                    R.anim.slide_out_right, R.anim.slide_out_right)
            .add(containerId, fragment, tag).addToBackStack(tag).commit();
    return true;
}

From source file:com.cylan.jiafeigou.utils.ActivityUtils.java

public static boolean addFragmentSlideInFromRight(FragmentManager fragmentManager, Fragment fragment,
        int containerId, boolean noStack) {
    final String tag = fragment.getClass().getSimpleName();
    Fragment f = fragmentManager.findFragmentByTag(tag);
    if (f != null && f.isVisible()) {
        return false;
    }/* www.ja  v a  2  s .  c om*/
    fragmentManager.beginTransaction().setCustomAnimations(R.anim.slide_right_in, R.anim.slide_out_left,
            R.anim.slide_out_right, R.anim.slide_out_right).add(containerId, fragment, tag).commit();
    return true;
}

From source file:com.cylan.jiafeigou.utils.ActivityUtils.java

public static boolean addFragmentSlideInFromLeft(FragmentManager fragmentManager, Fragment fragment,
        int containerId, boolean noStatck) {
    final String tag = fragment.getClass().getSimpleName();
    Fragment f = fragmentManager.findFragmentByTag(tag);
    if (f != null && f.isVisible()) {
        return false;
    }//from   w w  w  . ja  v  a  2s.c  o m
    fragmentManager.beginTransaction().setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_left,
            R.anim.slide_out_right, R.anim.slide_out_right).add(containerId, fragment, tag).commit();
    return true;
}

From source file:com.cylan.jiafeigou.utils.ActivityUtils.java

public static boolean addFragmentSlideInFromRight(FragmentManager fragmentManager, Fragment fragment,
        int containerId, String stackName) {
    final String tag = fragment.getClass().getSimpleName();
    Fragment f = fragmentManager.findFragmentByTag(tag);
    if (f != null && f.isVisible()) {
        return false;
    }//from   w ww.  j  a v  a 2s  .c  o m
    fragmentManager.beginTransaction()
            .setCustomAnimations(R.anim.slide_right_in, R.anim.slide_out_left, R.anim.slide_out_right,
                    R.anim.slide_out_right)
            .add(containerId, fragment, tag).addToBackStack(stackName).commitAllowingStateLoss();
    return true;
}

From source file:am.roadpolice.roadpolice.dialogs.DialogRateUs.java

private static void showDialog(final FragmentManager fragmentManager) {

    if (fragmentManager == null)
        return;/*from w w w.  j av a  2  s . c o  m*/

    DialogRateUs dialog = (DialogRateUs) fragmentManager.findFragmentByTag("rate_us");
    if (dialog == null)
        dialog = new DialogRateUs();

    dialog.setCancelable(false);
    // Start showing progress dialog fragment.
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.add(dialog, "rate_us");
    transaction.commitAllowingStateLoss();
}

From source file:cn.nekocode.rxactivityresult.compact.RxActivityResultCompact.java

private static Observable<ActivityResult> startActivityForResult(@NonNull FragmentManager fragmentManager,
        @NonNull final Intent intent, final int requestCode, @Nullable final Bundle options) {

    ResultHandleV4Fragment _fragment = (ResultHandleV4Fragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG);
    if (_fragment == null) {
        _fragment = new ResultHandleV4Fragment();

        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(_fragment, FRAGMENT_TAG);
        transaction.commit();/*w w  w  . j  ava2s .  co m*/

    } else if (_fragment.isDetached()) {
        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.attach(_fragment);
        transaction.commit();
    }

    final ResultHandleV4Fragment fragment = _fragment;
    return fragment.getIsAttachedBehavior().filter(new Predicate<Boolean>() {
        @Override
        public boolean test(@io.reactivex.annotations.NonNull Boolean isAttached) throws Exception {
            return isAttached;
        }
    }).flatMap(new Function<Boolean, ObservableSource<ActivityResult>>() {
        @Override
        public ObservableSource<ActivityResult> apply(@io.reactivex.annotations.NonNull Boolean aBoolean)
                throws Exception {
            fragment.startActivityForResult(intent, requestCode, options);
            return fragment.getResultPublisher();
        }
    }).filter(new Predicate<ActivityResult>() {
        @Override
        public boolean test(@io.reactivex.annotations.NonNull ActivityResult result) throws Exception {
            return result.getRequestCode() == requestCode;
        }
    });
}