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.learnncode.tabsample.BaseActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base);

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(getResources().getString(R.string.tab_one)),
            TabOneFrgment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(getResources().getString(R.string.tab_two)),
            TabTwoFrgment.class, null);

    mTabHost.setCurrentTabByTag("tag2");

    for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {

        TextView textView = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
        if (textView.getLayoutParams() instanceof RelativeLayout.LayoutParams) {

            RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) textView
                    .getLayoutParams();/*from ww  w. j  ava2  s. com*/
            params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
            params.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
            mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title).setLayoutParams(params);

        } else if (textView.getLayoutParams() instanceof LinearLayout.LayoutParams) {
            LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) textView
                    .getLayoutParams();
            params.gravity = Gravity.CENTER;
            mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title).setLayoutParams(params);
        }
    }

    mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {

            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            TabOneFrgment tabOneFrgment = (TabOneFrgment) fragmentManager.findFragmentByTag("tab1");
            TabTwoFrgment tabTwoFrgment = (TabTwoFrgment) fragmentManager.findFragmentByTag("tab2");
            android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            if (tabId.equalsIgnoreCase("tab1")) {
                if (tabOneFrgment != null) {
                    if (tabTwoFrgment != null) {
                        fragmentTransaction.hide(tabTwoFrgment);
                    }
                    fragmentTransaction.show(tabOneFrgment);
                }
            } else {
                if (tabTwoFrgment != null) {
                    if (tabOneFrgment != null) {
                        fragmentTransaction.hide(tabOneFrgment);
                    }
                    fragmentTransaction.show(tabTwoFrgment);
                }
            }
            fragmentTransaction.commit();
        }
    });
}

From source file:com.example.levelup.core.app.example1.LoginAndPaymentActivity.java

/**
 * Shows the login fragment. This also hooks in the {@link OnLoginListener} so that this
 * activity can be notified of when the user successfully logs in.
 *///from www  .j  a  v  a  2 s . c o  m
private void showLoginFragment() {
    FragmentManager fm = getSupportFragmentManager();
    LoginFragment loginFragment = (LoginFragment) fm.findFragmentByTag(LoginFragment.class.getName());

    FragmentTransaction transaction = fm.beginTransaction();

    if (loginFragment == null) {
        loginFragment = LoginFragment.newInstance();
    }

    if (!loginFragment.isAdded()) {
        transaction.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
        transaction.replace(android.R.id.content, loginFragment, LoginFragment.class.getName());
    }

    loginFragment.setOnLoginListener(mOnLoginListener);

    commitUiChange(transaction);
}

From source file:com.nextgis.ngm_clink_monitoring.fragments.LineListFragment.java

public void onLineClick(long lineRemoteId) {
    final FragmentManager fm = getActivity().getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    ObjectTypesFragment objectTypesFragment = (ObjectTypesFragment) fm
            .findFragmentByTag(FoclConstants.FRAGMENT_OBJECT_TYPES);

    if (null == objectTypesFragment) {
        objectTypesFragment = new ObjectTypesFragment();
    }/*from   ww  w  . ja  v  a2 s. c  o  m*/

    objectTypesFragment.setParams(lineRemoteId);

    ft.replace(R.id.main_fragment, objectTypesFragment, FoclConstants.FRAGMENT_OBJECT_TYPES);
    ft.addToBackStack(null);
    ft.commit();
}

From source file:cn.tycoon.lighttrans.fileManager.AbstractFilePickerActivity.java

@Override
@SuppressWarnings("unchecked")
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_filepicker);

    Intent intent = getIntent();// w  ww  .  ja  v  a2 s. c o m
    if (intent != null) {
        startPath = intent.getStringExtra(EXTRA_START_PATH);
        mode = intent.getIntExtra(EXTRA_MODE, mode);
        allowCreateDir = intent.getBooleanExtra(EXTRA_ALLOW_CREATE_DIR, allowCreateDir);
        allowMultiple = intent.getBooleanExtra(EXTRA_ALLOW_MULTIPLE, allowMultiple);
    }

    FragmentManager fm = getSupportFragmentManager();
    AbstractFilePickerFragment<T> fragment = (AbstractFilePickerFragment<T>) fm.findFragmentByTag(TAG);

    if (fragment == null) {
        fragment = getFragment(startPath, mode, allowMultiple, allowCreateDir);
    }

    if (fragment != null) {
        fm.beginTransaction().replace(R.id.fragment, fragment, TAG).commit();
    }

    // Default to cancelled
    setResult(Activity.RESULT_CANCELED);
}

From source file:com.google.android.apps.iosched2.ui.StarredActivity.java

/**
 * Build and add "vendors" tab./*from w w  w. j a v  a 2s.  co  m*/
 */
private void setupVendorsTab() {
    // TODO: this is very inefficient and messy, clean it up
    FrameLayout fragmentContainer = new FrameLayout(this);
    fragmentContainer.setId(R.id.fragment_vendors);
    fragmentContainer.setLayoutParams(
            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    ((ViewGroup) findViewById(android.R.id.tabcontent)).addView(fragmentContainer);

    final Intent intent = new Intent(Intent.ACTION_VIEW, Vendors.CONTENT_STARRED_URI);

    final FragmentManager fm = getSupportFragmentManager();

    mVendorsFragment = (VendorsFragment) fm.findFragmentByTag("vendors");
    if (mVendorsFragment == null) {
        mVendorsFragment = new VendorsFragment();
        mVendorsFragment.setArguments(intentToFragmentArguments(intent));
        fm.beginTransaction().add(R.id.fragment_vendors, mVendorsFragment, "vendors").commit();
    }

    // Vendors content comes from reused activity
    /*
     * removed vendors
     */
    if (Setup.FEATURE_VENDORS_ON) {
        mTabHost.addTab(mTabHost.newTabSpec(TAG_VENDORS).setIndicator(buildIndicator(R.string.starred_vendors))
                .setContent(R.id.fragment_vendors));
    }
}

From source file:com.bullmobi.message.ui.activities.EasyNotificationActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (mConfig.isWallpaperShown())
        setTheme(R.style.MaterialTheme_EasyNotification_Wallpaper);
    super.onCreate(savedInstanceState);
    mPresenter.onCreate(this);

    setContentView(R.layout.easynotification);

    // Initialize non-UI fragments.
    if (savedInstanceState == null) {
        initInternalFragments();//  w  w w  .ja v a 2s  .c o  m
    } else {

        // Find fragments.
        FragmentManager fm = getSupportFragmentManager();
        mPocketFragment = (PocketFragment) fm.findFragmentByTag(PocketFragment.TAG);
        // TODO: Maybe remove PocketFragment if active mode is disabled?
    }

    // Setup fragments.
    if (mPocketFragment != null) {
        mPocketFragment.setListener(this);
    }

    //   mPulsingThread = new PulsingThread(getContentResolver());
    //   mPulsingThread.start();
}

From source file:com.example.android.honeypad.ui.HomeActivity.java

/**
 * This method controls both fragments, instructing them to display a
 * certain note.//from   w ww  . java  2s  .co m
 * 
 * @param noteUri The {@link Uri} of the note to show. To create a new note,
 *            pass {@code null}.
 */
private void showNote(final Uri noteUri) {

    if (mTwoPaneView) {
        // check if the NoteEditFragment has been added
        FragmentManager fm = getSupportFragmentManager();
        NoteEditFragment edit = (NoteEditFragment) fm.findFragmentByTag(NOTE_EDIT_TAG);
        final boolean editNoteAdded = (edit != null);

        if (editNoteAdded) {
            if (edit.mCurrentNote != null && edit.mCurrentNote.equals(noteUri)) {
                // clicked on the currently selected note
                return;
            }

            NoteEditFragment editFrag = (NoteEditFragment) fm.findFragmentByTag(NOTE_EDIT_TAG);
            if (noteUri != null) {
                // load an existing note
                editFrag.loadNote(noteUri);
                NoteListFragment list = (NoteListFragment) fm.findFragmentById(R.id.list);
                list.setActivatedNote(Long.valueOf(noteUri.getLastPathSegment()));
            } else {
                // creating a new note - clear the form & list
                // activation
                if (editNoteAdded) {
                    editFrag.clear();
                }
                NoteListFragment list = (NoteListFragment) fm.findFragmentById(R.id.list);
                list.clearActivation();
            }
        } else {
            // add the NoteEditFragment to the container
            FragmentTransaction ft = fm.beginTransaction();
            edit = new NoteEditFragment();
            ft.add(R.id.note_detail_container, edit, NOTE_EDIT_TAG);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.commit();
            edit.loadNote(noteUri);
        }
    } else {
        startActivity(new Intent(NoteEditFragment.ACTION_VIEW_NOTE, noteUri));
    }
}

From source file:com.kubotaku.android.code4kyoto5374.MainActivity.java

private void removeSelectHomeView() {
    final FragmentManager fm = getSupportFragmentManager();
    if (fm.findFragmentByTag(HomeSelectFragment.TAG) != null) {
        final FragmentTransaction trans = fm.beginTransaction();
        trans.remove(fm.findFragmentByTag(HomeSelectFragment.TAG));
        trans.commitAllowingStateLoss();
    }/*w  w w.  j a v  a  2 s . co m*/
}

From source file:com.kubotaku.android.code4kyoto5374.MainActivity.java

private void removeGarbageDaysView() {
    final FragmentManager fm = getSupportFragmentManager();
    if (fm.findFragmentByTag(GarbageCollectDaysFragment.TAG) != null) {
        final FragmentTransaction trans = fm.beginTransaction();
        trans.remove(fm.findFragmentByTag(GarbageCollectDaysFragment.TAG));
        trans.commitAllowingStateLoss();
    }/*from  w ww  . j av  a  2 s  .  c  om*/
}

From source file:com.kubotaku.android.code4kyoto5374.MainActivity.java

private void showSelectHomeView(int mode) {
    final FragmentManager fm = getSupportFragmentManager();
    if (fm.findFragmentByTag(HomeSelectFragment.TAG) == null) {
        final FragmentTransaction trans = fm.beginTransaction();
        final HomeSelectFragment fragment = HomeSelectFragment.newInstance(mode);
        trans.add(R.id.view_holder, fragment, HomeSelectFragment.TAG);
        trans.commitAllowingStateLoss();
    }/*from  w ww  .j av a 2 s . c  o  m*/
}