Example usage for android.app FragmentManager findFragmentByTag

List of usage examples for android.app FragmentManager findFragmentByTag

Introduction

In this page you can find the example usage for android.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.blue.leaves.util.task.SugarTask.java

private void registerHookToContext(@NonNull Activity activity) {
    FragmentManager manager = activity.getFragmentManager();

    HookFragment hookFragment = (HookFragment) manager.findFragmentByTag(TAG_HOOK);
    if (hookFragment == null) {
        hookFragment = new HookFragment();
        manager.beginTransaction().add(hookFragment, TAG_HOOK).commitAllowingStateLoss();
    }/* w  w w .j a  v a 2s. c o  m*/
}

From source file:com.blue.leaves.util.task.SugarTask.java

private void unregisterHookToContext(@NonNull Activity activity) {
    FragmentManager manager = activity.getFragmentManager();

    HookFragment hookFragment = (HookFragment) manager.findFragmentByTag(TAG_HOOK);
    if (hookFragment != null) {
        hookFragment.postEnable = false;
        manager.beginTransaction().remove(hookFragment).commitAllowingStateLoss();
    }//from   w  w  w .  java  2 s .c  om
}

From source file:org.iota.wallet.ui.activity.MainActivity.java

/**
 * Shows a fragment and hides the old one if there was a fragment previously visible
 *///from   ww  w  . j av a 2s .  c om
private void showFragment(Fragment fragment, boolean addToBackStack, boolean killFragments) {

    if (fragment == null) {
        // Do nothing
        return;
    }

    FragmentManager fragmentManager = getFragmentManager();
    Fragment currentFragment = fragmentManager.findFragmentByTag(currentFragmentTag);

    if (currentFragment != null && currentFragment.getClass().getName().equals(fragment.getClass().getName())) {
        // Fragment already shown, do nothing
        return;
    }

    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

    if (killFragments) {
        Class[] fragmentsToKill = { AboutFragment.class, GenerateQRCodeFragment.class, NeighborsFragment.class,
                NodeInfoFragment.class, PasswordLoginFragment.class, QRScannerFragment.class,
                SeedLoginFragment.class, SettingsFragment.class, TangleExplorerTabFragment.class,
                NewTransferFragment.class, WalletAddressesFragment.class, WalletTabFragment.class,
                WalletTransfersFragment.class };
        for (Class fragmentClass : fragmentsToKill) {
            String tag = fragmentClass.getSimpleName();
            if (tag.equals(fragment.getClass().getSimpleName())) {
                continue;
            }
            Fragment fragmentToKill = fragmentManager.findFragmentByTag(tag);
            if (fragmentToKill != null) {
                fragmentTransaction.remove(fragmentToKill);
            }
        }
    }

    fragmentTransaction.setCustomAnimations(R.animator.fade_in, R.animator.fade_out, R.animator.fade_in,
            R.animator.fade_out);

    if (currentFragment != null) {
        // Hide old fragment
        fragmentTransaction.hide(currentFragment);
    }

    String tag = fragment.getClass().getSimpleName();
    Fragment cachedFragment = fragmentManager.findFragmentByTag(tag);
    if (cachedFragment != null) {
        // Cached fragment available
        fragmentTransaction.show(cachedFragment);
    } else {
        fragmentTransaction.add(FRAGMENT_CONTAINER_ID, fragment, tag);
    }
    if (addToBackStack) {
        fragmentTransaction.addToBackStack(null);
    }
    fragmentTransaction.commit();

    if (fragment instanceof OnBackPressedClickListener) {
        onBackPressedClickListener = (OnBackPressedClickListener) fragment;
    } else
        onBackPressedClickListener = null;

    // setChecked if open from WalletItemDialog
    if (fragment instanceof TangleExplorerTabFragment)
        navigationView.getMenu().findItem(R.id.nav_tangle_explorer).setChecked(true);

    currentFragmentTag = tag;
}

From source file:com.blue.leaves.util.task.SugarTask.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void registerHookToContext(@NonNull Fragment fragment) {
    FragmentManager manager = fragment.getChildFragmentManager();

    HookFragment hookFragment = (HookFragment) manager.findFragmentByTag(TAG_HOOK);
    if (hookFragment == null) {
        hookFragment = new HookFragment();
        manager.beginTransaction().add(hookFragment, TAG_HOOK).commitAllowingStateLoss();
    }// w  w w . j a v a 2 s. co  m
}

From source file:com.blue.leaves.util.task.SugarTask.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void unregisterHookToContext(@NonNull Fragment fragment) {
    FragmentManager manager = fragment.getChildFragmentManager();

    HookFragment hookFragment = (HookFragment) manager.findFragmentByTag(TAG_HOOK);
    if (hookFragment != null) {
        hookFragment.postEnable = false;
        manager.beginTransaction().remove(hookFragment).commitAllowingStateLoss();
    }/*from ww  w.  j  ava  2  s. c om*/
}

From source file:com.github.chenxiaolong.dualbootpatcher.MainActivity.java

private void showFragment() {
    FragmentManager fm = getFragmentManager();

    Fragment prevRoms = fm.findFragmentByTag(SwitcherListFragment.TAG);
    Fragment prevPatchFile = fm.findFragmentByTag(PatchFileFragment.TAG);
    Fragment prevFreeSpace = fm.findFragmentByTag(FreeSpaceFragment.TAG);
    Fragment prevAbout = fm.findFragmentByTag(AboutFragment.TAG);

    FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(R.animator.fragment_in, 0);

    switch (mFragment) {
    case FRAGMENT_ROMS:
        mTitle = R.string.title_roms;/* ww w. j  a  v  a2s.c  o m*/
        updateTitle();

        if (prevRoms == null) {
            Fragment f = SwitcherListFragment.newInstance();
            ft.add(R.id.content_frame, f, SwitcherListFragment.TAG);
        } else {
            ft.show(prevRoms);
        }

        break;

    case FRAGMENT_PATCH_FILE:
        mTitle = R.string.title_patch_zip;
        updateTitle();

        if (prevPatchFile == null) {
            Fragment f = PatchFileFragment.newInstance();
            ft.add(R.id.content_frame, f, PatchFileFragment.TAG);
        } else {
            ft.show(prevPatchFile);
        }

        break;

    case FRAGMENT_FREE_SPACE:
        mTitle = R.string.title_free_space;
        updateTitle();

        if (prevFreeSpace == null) {
            Fragment f = FreeSpaceFragment.newInstance();
            ft.add(R.id.content_frame, f, FreeSpaceFragment.TAG);
        } else {
            ft.show(prevFreeSpace);
        }

        break;

    case FRAGMENT_ABOUT:
        mTitle = BuildConfig.APP_NAME_RESOURCE;
        updateTitle();

        if (prevAbout == null) {
            Fragment f = AboutFragment.newInstance();
            ft.add(R.id.content_frame, f, AboutFragment.TAG);
        } else {
            ft.show(prevAbout);
        }

        break;

    default:
        throw new IllegalStateException("Invalid fragment ID");
    }

    ft.commit();
}

From source file:com.android.mail.ui.TwoPaneController.java

@Override
public void launchFragment(final Fragment fragment, final int selectPosition) {
    final int containerViewId = TwoPaneLayout.MISCELLANEOUS_VIEW_ID;

    final FragmentManager fragmentManager = mActivity.getFragmentManager();
    if (fragmentManager.findFragmentByTag(TAG_CUSTOM_FRAGMENT) == null) {
        final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.replace(containerViewId, fragment, TAG_CUSTOM_FRAGMENT);
        mMiscellaneousViewTransactionId = fragmentTransaction.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }//from   w  w w .j  a  v  a 2  s  .  c  o m

    if (selectPosition >= 0) {
        getConversationListFragment().setRawSelected(selectPosition, true);
    }
}

From source file:com.tassadar.multirommgr.MainActivity.java

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

    if (Build.VERSION.SDK_INT == 20) {
        showDeprecatedLAlert();//  w  ww. ja v a2  s . c  o m
        return;
    }

    setContentView(R.layout.activity_main);

    // This activity is using different background color, which would cause overdraw
    // of the whole area, so disable the default background
    getWindow().setBackgroundDrawable(null);

    Utils.installHttpCache(this);
    PreferenceManager.setDefaultValues(this, R.xml.settings, false);

    m_srLayout = (MultiROMSwipeRefreshLayout) findViewById(R.id.refresh_layout);
    m_srLayout.setOnRefreshListener(this);

    m_curFragment = -1;

    m_fragmentTitles = getResources().getStringArray(R.array.main_fragment_titles);
    m_drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    m_drawerList = (ListView) findViewById(R.id.left_drawer);

    String[] fragmentClsNames = new String[MainFragment.MAIN_FRAG_CNT];
    for (int i = 0; i < fragmentClsNames.length; ++i)
        fragmentClsNames[i] = MainFragment.getFragmentClass(i).getName();

    m_fragments = new MainFragment[MainFragment.MAIN_FRAG_CNT];
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction t = fragmentManager.beginTransaction();
    for (int i = 0; i < m_fragments.length; ++i) {
        m_fragments[i] = (MainFragment) fragmentManager.findFragmentByTag(fragmentClsNames[i]);
        if (m_fragments[i] == null) {
            m_fragments[i] = MainFragment.newFragment(i);
            t.add(R.id.content_frame, m_fragments[i], fragmentClsNames[i]);
        }
        t.hide(m_fragments[i]);
    }
    t.commit();

    // Set the adapter for the list view
    m_drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, m_fragmentTitles));
    // Set the list's click listener
    m_drawerList.setOnItemClickListener(new DrawerItemClickListener());

    m_drawerTitle = getText(R.string.app_name);
    m_drawerToggle = new ActionBarDrawerToggle(this, m_drawerLayout, R.string.drawer_open,
            R.string.drawer_close) {
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(m_title);
        }

        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(m_drawerTitle);
        }
    };
    m_drawerLayout.setDrawerListener(m_drawerToggle);

    final ActionBar bar = getSupportActionBar();
    if (bar != null) {
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setHomeButtonEnabled(true);
    }

    if (getIntent().hasExtra(INTENT_EXTRA_SHOW_ROM_LIST)
            && getIntent().getBooleanExtra(INTENT_EXTRA_SHOW_ROM_LIST, false)) {
        getIntent().removeExtra(INTENT_EXTRA_SHOW_ROM_LIST);
        selectItem(1);
    } else if (savedInstanceState != null) {
        selectItem(savedInstanceState.getInt("curFragment", 0));
    } else {
        selectItem(0);
    }
}

From source file:com.zte.permissioncontrol.ui.PermissionControlPageActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.permission_control_pages);
    mEmptyView = (FrameLayout) findViewById(R.id.empty_view);
    mSavedInstanceState = savedInstanceState;
    // add the switch on Action bar
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mSwitch = (Switch) inflater.inflate(R.layout.imageswitch_layout, null);
    final int padding = getResources().getDimensionPixelSize(R.dimen.action_bar_switch_padding);
    mSwitch.setPadding(0, 0, padding, 0);
    getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
    getActionBar().setCustomView(mSwitch, new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.END));
    mSwitch.setOnCheckedChangeListener(this);

    // hide fragment firstly , then update it in onResume() according to switch status
    final FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();

    mPermissionsFragment = (PermissionsFragment) fragmentManager.findFragmentByTag(mPermissionsTag);
    mAppsFragment = (AppsFragment) fragmentManager.findFragmentByTag(mAppsTag);

    if (mPermissionsFragment == null) {
        mPermissionsFragment = new PermissionsFragment();
        mAppsFragment = new AppsFragment();
        transaction.add(R.id.tab_pager, mPermissionsFragment, mPermissionsTag);
        transaction.add(R.id.tab_pager, mAppsFragment, mAppsTag);
    }//from w w w  .  j a v a2s  .  co m

    transaction.hide(mPermissionsFragment);
    transaction.hide(mAppsFragment);
    transaction.commit();
    fragmentManager.executePendingTransactions();

    // set page adapter
    mTabPager = (ViewPager) findViewById(R.id.tab_pager);
    mTabPagerAdapter = new TabPagerAdapter();
    mTabPager.setAdapter(mTabPagerAdapter);
    mTabPager.setOnPageChangeListener(mTabPagerListener);

    // Configure action bar
    mActionBarAdapter = new ActionBarAdapter(this, this, getActionBar());

    boolean isShow = PermControlUtils.isPermControlOn(this);
    Log.d(TAG, "oncreate(), isShow " + isShow);

}

From source file:com.blue.leaves.util.task.SugarTask.java

private void registerHookToContext(@NonNull android.support.v4.app.Fragment fragment) {
    android.support.v4.app.FragmentManager manager = fragment.getChildFragmentManager();

    HookSupportFragment hookSupportFragment = (HookSupportFragment) manager.findFragmentByTag(TAG_HOOK);
    if (hookSupportFragment == null) {
        hookSupportFragment = new HookSupportFragment();
        manager.beginTransaction().add(hookSupportFragment, TAG_HOOK).commitAllowingStateLoss();
    }/*  w  ww.  j  a va2s.c  om*/
}