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

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

Introduction

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

Prototype

public abstract Fragment findFragmentByTag(String tag);

Source Link

Document

Finds a fragment that was identified by the given tag either when inflated from XML or as supplied when added in a transaction.

Usage

From source file:com.mbientlab.metawear.app.NavigationActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    if (currentFragment != null) {
        transaction.detach(currentFragment);
    }/*from w ww  . ja  v  a  2  s  .c  om*/

    String newFragmentTag = FRAGMENT_TAGS.get(position);
    currentFragment = fragmentManager.findFragmentByTag(newFragmentTag);

    if (currentFragment == null) {
        try {
            currentFragment = FRAGMENT_CLASSES.get(newFragmentTag).getConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException("Cannot instantiate fragment", e);
        }

        transaction.add(R.id.container, currentFragment, newFragmentTag);
    }
    mTitle = newFragmentTag;
    restoreActionBar();
    transaction.attach(currentFragment).commit();
}

From source file:com.hippo.scene.StageActivity.java

public SceneFragment findSceneByTag(String tag) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(tag);
    if (fragment != null) {
        return (SceneFragment) fragment;
    } else {//from w  w w.j a  v  a 2 s. c o m
        return null;
    }
}

From source file:com.abid_mujtaba.fetchheaders.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);/*from w  w  w .j  a  va2  s  . c  o  m*/

    if (savedInstanceState != null) // If the passed in state information bundle is non-empty we expect it to contain the saved value of fShowSeen. We also pass in a default value.
    {
        fShowSeen = savedInstanceState.getBoolean(BUNDLE_FLAG_SHOW_SEEN, false);
    }

    scrollList = (LinearLayout) findViewById(R.id.scrollList);

    if (mTTS == null) // If onCreate is called multiple times we do NOT want to create multiple TextToSpeech objects
    {
        mTTS = new TextToSpeech(this, this);
    }

    if (Account.numberOfAccounts() > 0) // Accounts have been specified
    {
        TextView tvEmpty = (TextView) findViewById(R.id.txtNoAccounts); // We start by removing the No Accounts view since accounts are present
        scrollList.removeView(tvEmpty);

        FragmentManager fM = getSupportFragmentManager();
        FragmentTransaction fT = fM.beginTransaction();

        for (int ii = 0; ii < Account.numberOfAccounts(); ii++) {
            String tag = "TAG_" + ii; // This is the tag we will use to get a handle on the fragment in the FragmentManager

            AccountFragment aF = (AccountFragment) fM.findFragmentByTag(tag); // We attempt to access the fragment via the specified tag

            if (aF == null) // This indicates that the Fragment does not exist yet so we create it. It has setRetainInstance(true) so it persists across configuration changes.
            {
                aF = AccountFragment.newInstance(ii);

                fT.add(R.id.scrollList, aF, tag); // Note: The addition to the scrollList only happens when aF == null, which happens when the persistent fragment has not been created yet
            } //       Since Views retain state across config changes the scrollList remembers that it has fragments added to it

            mFragments.add(aF);
        }

        fT.commit();
    }
}

From source file:com.hippo.scene.StageActivity.java

public void refreshTopScene() {
    int index = mSceneTagList.size() - 1;
    if (index < 0) {
        return;// w w  w .ja  v  a 2 s.c o  m
    }
    String tag = mSceneTagList.get(index);

    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(tag);

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.detach(fragment);
    transaction.attach(fragment);
    transaction.commitAllowingStateLoss();
}

From source file:com.nextgis.woody.activity.MainActivity.java

public void hideTreeDetails() {
    FragmentManager fm = getSupportFragmentManager();
    MapFragment mapFragment = (MapFragment) fm.findFragmentByTag(Constants.FRAGMENT_MAP);
    if (mapFragment != null)
        mapFragment.unselectGeometry();//from   www.  j  a va 2  s  .  c o  m
    findViewById(R.id.add_tree).setVisibility(View.VISIBLE);
    findViewById(R.id.tree_details).setVisibility(View.GONE);
}

From source file:com.kasungunathilaka.sarigama.ui.HomeActivity.java

protected void setFragment(Fragment fragment) {

    String backStateName = fragment.getClass().getName();
    String fragmentTag = backStateName;

    android.support.v4.app.FragmentManager manager = getSupportFragmentManager();

    boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
    if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) { //fragment not in back stack, create it.
        android.support.v4.app.FragmentTransaction ft = manager.beginTransaction();
        if (backStateName.contentEquals(PlayerFragment.class.getName())) {
            ft.setCustomAnimations(R.anim.slide_in_below, R.anim.slide_out_top, R.anim.slide_in_top,
                    R.anim.slide_out_below);
        } else if (backStateName.contentEquals(MainFragment.class.getName())) {
            ft.setCustomAnimations(android.R.anim.fade_in, R.anim.slide_out_right);
        } else if (backStateName.contentEquals(ConnectionFailFragment.class.getName())) {
            ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
        } else {/*from ww w  .j  a  va  2s  . c  om*/
            ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left,
                    R.anim.slide_out_right);
        }
        ft.replace(R.id.fragment_layout, fragment, fragmentTag);
        if (!backStateName.contentEquals(MainFragment.class.getName())) {
            ft.addToBackStack(backStateName);
        }
        ft.commit();
    }
}

From source file:com.nextgis.woody.activity.MainActivity.java

public void editTree(long i) {
    FragmentManager fm = getSupportFragmentManager();
    MapFragment mapFragment = (MapFragment) fm.findFragmentByTag(Constants.FRAGMENT_MAP);
    if (mapFragment != null)
        mapFragment.unselectGeometry();/*w  w  w.  ja  va 2 s.c  o m*/

    Intent intent = new Intent(this, EditActivity.class);
    intent.putExtra(Constants.FEATURE_ID, i);
    if (mapFragment != null) {
        GeoPoint pt = mapFragment.getCenter();
        intent.putExtra(SettingsConstants.KEY_PREF_SCROLL_X, pt.getX());
        intent.putExtra(SettingsConstants.KEY_PREF_SCROLL_Y, pt.getY());
    }
    startActivity(intent);
}

From source file:com.nextgis.woody.activity.MainActivity.java

private void handleFBResult(int requestCode, int resultCode, Intent data) {
    FragmentManager fm = getSupportFragmentManager();
    LoginFragment ngwLoginFragment = (LoginFragment) fm.findFragmentByTag(Constants.FRAGMENT_LOGIN);
    if (ngwLoginFragment != null) {
        CallbackManager callbackManager = ngwLoginFragment.getCallbackManager();
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }/*w w w  .  j  a v a2 s  . c  o m*/
}

From source file:com.customprogrammingsolutions.MediaStreamer.MainActivity.java

private void showFavoritesDialog(boolean addFromRecents, boolean addFromUser, boolean edit, String name,
        String url, long rowId) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    FavoritesDialog tPrev = (FavoritesDialog) fragmentManager.findFragmentByTag("favorite_dialog");
    if (tPrev != null)
        fragmentTransaction.remove(tPrev);

    FavoritesDialog favoritesDialog = FavoritesDialog.newInstance(addFromRecents, addFromUser, edit, name, url,
            rowId, getSupportLoaderManager(), this);
    favoritesDialog.show(fragmentTransaction, "favorite_dialog");
}

From source file:com.grayfox.android.app.fragment.RecommendedRouteFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    FragmentManager fragmentManager = getChildFragmentManager();
    SupportMapFragment fragment = (SupportMapFragment) fragmentManager.findFragmentByTag(MAP_FRAGMENT_TAG);
    if (fragment == null) {
        fragment = SupportMapFragment.newInstance();
        fragmentManager.beginTransaction().replace(R.id.map_container, fragment, MAP_FRAGMENT_TAG).commit();
    }//w  ww  . ja  v a2s  .  co  m
    directionsMenu.setIconAnimated(false);
    walkingDirectionsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            directionsMenu.close(true);
            if (routeBuilderTask != null && !routeBuilderTask.isActive())
                recalculateRoute(TravelMode.WALKING);
        }
    });
    transitDirectionsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            directionsMenu.close(true);
            if (routeBuilderTask != null && !routeBuilderTask.isActive())
                recalculateRoute(TravelMode.TRANSIT);
        }
    });
    drivingDirectionsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            directionsMenu.close(true);
            if (routeBuilderTask != null && !routeBuilderTask.isActive())
                recalculateRoute(TravelMode.DRIVING);
        }
    });
    bikeDirectionsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            directionsMenu.close(true);
            if (routeBuilderTask != null && !routeBuilderTask.isActive())
                recalculateRoute(TravelMode.BICYCLING);
        }
    });
    routeList.setLayoutManager(new LinearLayoutManager(getActivity()));
    routeList.setItemAnimator(null);
    poiRouteAdapter = new PoiRouteAdapter(getCurrentLocationArg());
    poiRouteAdapter.setOnDeleteItemListener(new PoiRouteAdapter.OnDeleteItemListener() {
        @Override
        public void onDelete(Poi poi, int position) {
            onDeletePoiInRoute(position);
        }
    });
    routeList.setAdapter(poiRouteAdapter);
    final DragSortRecycler dragSortRecycler = new DragSortRecycler();
    dragSortRecycler.setViewHandleId(R.id.reorder_icon);
    dragSortRecycler.setOnItemMovedListener(new DragSortRecycler.OnItemMovedListener() {
        @Override
        public void onItemMoved(int from, int to) {
            onReorderPoisInRoute(from, to);
        }
    });
    routeList.addItemDecoration(dragSortRecycler);
    routeList.addOnItemTouchListener(dragSortRecycler);
    verticalShowAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.vertical_show);
    verticalHideAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.vertical_hide);
    fragment.getMapAsync(this);
}