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.alex.utils.FragmentNavigator.java

public static void moveTo(FragmentManager fragmentManager, @NonNull Fragment fragment, @IdRes int containerId,
        boolean addToBackStack) {

    Fragment current = fragmentManager.findFragmentById(containerId);

    if (current == fragment) {
        return;/*from  w ww  .  j av a 2 s .c  om*/
    }

    final String tag = fragment.getTag();

    FragmentTransaction transaction = fragmentManager.beginTransaction();

    if (current == null) {
        transaction.add(containerId, fragment, tag);
    } else {
        transaction.replace(containerId, fragment, tag);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        if (addToBackStack) {
            transaction.addToBackStack(tag);
        }
    }
    transaction.commit();

    //        if (fragment == null) {
    //            try {
    //                target = Fragment.instantiate(context, fragment.getName(), args);
    //            } catch (Exception e) {
    //                // ignore
    //            }
    //            if (target == null) {
    //                return;
    //            }
    //
    //
    //        } else {
    //            if (current == target) {
    //                return;
    //            }
    //            // set result
    //            Intent intent = new Intent();
    //            if (args != null) {
    //                intent.putExtras(args);
    //            }
    //            target.onActivityResult(REQUEST_CODE, Activity.RESULT_OK, intent);
    //            boolean result = fragmentManager.popBackStackImmediate(tag, 0);
    //            if (!result) {
    //                fragmentManager.popBackStackImmediate(0, POP_BACK_STACK_INCLUSIVE);
    //            }
    //        }

}

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

public static void loadMostSearched(final AppCompatActivity appCompatActivity) {

    final ArrayList<Stop> stopsList = new ArrayList<>();
    final ArrayList<String> stopsNameList = new ArrayList<>();

    new AsyncTask<Void, Void, Void>() {

        @Override/*  w ww.java2s  . c  om*/
        protected void onPreExecute() {
            super.onPreExecute();
            if (!ConnectionsHandler.isNetworkPresent(appCompatActivity)) {
                this.cancel(true);
                stopsNameList.add("Connessione dati non presente");
                ListView listView_searched = (ListView) appCompatActivity
                        .findViewById(R.id.textView_mostSerched);
                ArrayAdapter<String> adapter = new ArrayAdapter<>(appCompatActivity.getApplicationContext(),
                        R.layout.text_view, stopsNameList);
                listView_searched.setAdapter(adapter);
            }
        }

        @Override
        protected Void doInBackground(Void... params) {

            Stop stop;
            String url = "http://www.aleiacampo.com/stops.php?clicked=10";
            WebServerHandler webServerHandler = new WebServerHandler();
            String jsonStr = webServerHandler.getJSONData(url);
            try {
                JSONObject jsonObject = new JSONObject(jsonStr);
                JSONArray stopsJSON = jsonObject.getJSONArray("bus_stops");
                for (int i = 0; i < stopsJSON.length(); i++) {
                    JSONObject bus_stop = stopsJSON.getJSONObject(i);
                    stop = new Stop(bus_stop.getInt("id_line"), bus_stop.getInt("id_stop"),
                            bus_stop.getString("name_line"), bus_stop.getString("name_stop"));
                    stopsList.add(stop);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            for (Stop stop : stopsList) {
                stopsNameList.add("Linea " + stop.idLine + " - " + stop.nameStop);
            }

            ListView listView_searched = (ListView) appCompatActivity.findViewById(R.id.textView_mostSerched);
            ArrayAdapter<String> adapter = new ArrayAdapter<>(appCompatActivity.getApplicationContext(),
                    R.layout.text_view, stopsNameList);
            listView_searched.setAdapter(adapter);
            listView_searched.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

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

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

                }
            });
        }
    }.execute();
}

From source file:can.yrt.onebusaway.SituationFragment.java

static void show(SherlockFragmentActivity activity, ObaSituation situation) {
    FragmentManager fm = activity.getSupportFragmentManager();

    Bundle args = new Bundle();
    args.putString(TITLE, situation.getSummary());
    // We don't use the stop name map here...we want the actual stop name.
    args.putString(DESCRIPTION, situation.getDescription());

    // Create the list fragment and add it as our sole content.
    SituationFragment content = new SituationFragment();
    content.setArguments(args);//from   www. j av  a  2s  .c  o m

    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(android.R.id.content, content);
    ft.addToBackStack(null);
    ft.commit();
}

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

/**
 * Display a dialog which asks if the user wants to add the given show to his show database. If
 * necessary an AsyncTask will be started which takes care of adding the show.
 *///  w  ww .  j  a v  a2s.  c  o  m
public static void showAddDialog(SearchResult show, 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(TAG);
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = AddShowDialogFragment.newInstance(show);
    newFragment.show(ft, TAG);
}

From source file:com.apptentive.android.sdk.module.engagement.interaction.fragment.ApptentiveBaseFragment.java

public static void removeFragment(FragmentManager fragmentManager, Fragment fragment) {
    fragmentManager.beginTransaction().remove(fragment).commit();
}

From source file:android.com.example.contactslist.util.ImageCache.java

/**
 * Locate an existing instance of this Fragment or if not found, create and
 * add it using FragmentManager./*from  w w w .  ja v  a  2s .co m*/
 *
 * @param fm The FragmentManager manager to use.
 * @return The existing instance of the Fragment or the new instance if just
 *         created.
 */
public static RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
    // Check to see if we have retained the worker fragment.
    RetainFragment mRetainFragment = (RetainFragment) fm.findFragmentByTag(TAG);

    // If not retained (or first time running), we need to create and add it.
    if (mRetainFragment == null) {
        mRetainFragment = new RetainFragment();
        fm.beginTransaction().add(mRetainFragment, TAG).commitAllowingStateLoss();
    }

    return mRetainFragment;
}

From source file:Main.java

public static void replaceFragment(FragmentManager manager, Class<? extends Fragment> fragmentClass,
        boolean isAddToBackStack) {

    Fragment fragment = manager.findFragmentByTag(fragmentClass.getSimpleName());

    if (null == fragment) {
        try {/*from w  w w  .  jav  a2s  .com*/

            fragment = fragmentClass.newInstance();

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    FragmentTransaction ft = manager.beginTransaction();
    ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in,
            android.R.anim.fade_out);
    if (!fragment.isAdded()) {
        ft.replace(android.R.id.content, fragment, fragment.getClass().getSimpleName());
        if (isAddToBackStack) {
            ft.addToBackStack(null);
        }
    }
    ft.commit();

}

From source file:can.yrt.onebusaway.ReportStopProblemFragment.java

static void show(SherlockFragmentActivity activity, ObaStop stop) {
    FragmentManager fm = activity.getSupportFragmentManager();

    Bundle args = new Bundle();
    args.putString(STOP_ID, stop.getId());
    // We don't use the stop name map here...we want the actual stop name.
    args.putString(STOP_NAME, stop.getName());

    // Create the list fragment and add it as our sole content.
    ReportStopProblemFragment content = new ReportStopProblemFragment();
    content.setArguments(args);/*from ww w.  ja  va  2s  .  com*/

    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(android.R.id.content, content);
    ft.addToBackStack(null);
    ft.commit();
}

From source file:com.app.chasebank.contactslist.util.ImageCache.java

/**
 * Locate an existing instance of this Fragment or if not found, create and
 * add it using FragmentManager.//from w  ww  .  j a  va 2 s  . co  m
 *
 * @param fm The FragmentManager manager to use.
 * @return The existing instance of the Fragment or the new instance if just
 *         created.
 */
public static RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
    // Check to see if we have retained the worker fragment.
    RetainFragment mRetainFragment = (com.app.chasebank.contactslist.util.ImageCache.RetainFragment

    ) fm.findFragmentByTag(TAG);

    // If not retained (or first time running), we need to create and add it.
    if (mRetainFragment == null) {
        mRetainFragment = new RetainFragment();
        fm.beginTransaction().add(mRetainFragment, TAG).commitAllowingStateLoss();
    }

    return mRetainFragment;
}

From source file:com.android.volley.cache.BitmapCache.java

/**
 * Locate an existing instance of this Fragment or if not found, create and
 * add it using FragmentManager.//from  w w  w  .  java 2s. c  o m
 *
 * @param fm The FragmentManager manager to use.
 * @param fragmentTag The tag of the retained fragment (should be unique for each memory
 *                    cache that needs to be retained).
 * @return The existing instance of the Fragment or the new instance if just
 *         created.
 */
private static RetainFragment getRetainFragment(FragmentManager fm, String fragmentTag) {
    // Check to see if we have retained the worker fragment.
    RetainFragment mRetainFragment = (RetainFragment) fm.findFragmentByTag(fragmentTag);

    // If not retained (or first time running), we need to create and add it.
    if (mRetainFragment == null) {
        mRetainFragment = new RetainFragment();
        fm.beginTransaction().add(mRetainFragment, fragmentTag).commitAllowingStateLoss();
    }

    return mRetainFragment;
}