Example usage for android.app FragmentManager beginTransaction

List of usage examples for android.app FragmentManager beginTransaction

Introduction

In this page you can find the example usage for android.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.codefororlando.transport.MapsActivity.java

private void removeSelectableItemFragment() {
    final Fragment selectableItemFragment = getFragmentManager().findFragmentByTag(ISelectableItemFragment.TAG);
    if (selectableItemFragment != null) {
        final View view = selectableItemFragment.getView();
        if (view == null) {
            return;
        }//  w w  w  . j a v a  2s .  co m

        final int animationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);

        {
            final Animation animation = new TranslateAnimation(0, 0, 0, view.getHeight());
            animation.setDuration(animationDuration);
            animation.setInterpolator(new AccelerateDecelerateInterpolator());
            filterView.startAnimation(animation);
        }
        {
            final FragmentManager fragmentManager = getFragmentManager();
            final Animation animation = new TranslateAnimation(0, 0, 0, view.getHeight());
            animation.setDuration(animationDuration);
            animation.setAnimationListener(new EmptyAnimationListener() {
                @Override
                public void onAnimationEnd(Animation animation) {
                    try {
                        fragmentManager.beginTransaction().remove(selectableItemFragment)
                                .commitAllowingStateLoss();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            view.startAnimation(animation);
        }
    }
}

From source file:by.zatta.pilight.MainActivity.java

private void openFragment(BaseFragment mBaseFragment2) {
    Log.v(TAG, "openFragment");
    if (mBaseFragment2 != null) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        fm.popBackStack();//  www .  ja  va  2s .  c om
        ft.replace(R.id.fragment_main, mBaseFragment2, mBaseFragment2.getName());
        ft.commit();
    }
}

From source file:by.zatta.pilight.MainActivity.java

private void openDialogFragment(DialogFragment dialogStandardFragment) {
    if (dialogStandardFragment != null) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        DialogFragment prev = (DialogFragment) fm.findFragmentByTag("dialog");
        if (prev != null) {
            prev.dismiss();//  w  w  w. ja  v  a  2s  .  c o m
        }
        try {
            dialogStandardFragment.show(ft, "dialog");
        } catch (IllegalStateException e) {
            Log.w(TAG, "activity wasn't yet made");
        }
    }
}

From source file:android.support.v17.leanback.app.GuidedStepFragment.java

/**
 * Adds the specified GuidedStepFragment as content of Activity; no backstack entry is added so
 * the activity will be dismissed when BACK key is pressed.  The method is typically called in
 * Activity.onCreate() when savedInstanceState is null.  When savedInstanceState is not null,
 * the Activity is being restored,  do not call addAsRoot() to duplicate the Fragment restored
 * by FragmentManager.// w w  w.  j av a2  s .  c o  m
 * {@link #UI_STYLE_ACTIVITY_ROOT} is assigned.
 *
 * Note: currently fragments added using this method must be created programmatically rather
 * than via XML.
 * @param activity The Activity to be used to insert GuidedstepFragment.
 * @param fragment The GuidedStepFragment to be inserted into the fragment stack.
 * @param id The id of container to add GuidedStepFragment, can be android.R.id.content.
 * @return The ID returned by the call FragmentTransaction.commit, or -1 there is already
 *         GuidedStepFragment.
 */
public static int addAsRoot(Activity activity, GuidedStepFragment fragment, int id) {
    // Workaround b/23764120: call getDecorView() to force requestFeature of ActivityTransition.
    activity.getWindow().getDecorView();
    FragmentManager fragmentManager = activity.getFragmentManager();
    if (fragmentManager.findFragmentByTag(TAG_LEAN_BACK_ACTIONS_FRAGMENT) != null) {
        Log.w(TAG, "Fragment is already exists, likely calling "
                + "addAsRoot() when savedInstanceState is not null in Activity.onCreate().");
        return -1;
    }
    FragmentTransaction ft = fragmentManager.beginTransaction();
    fragment.setUiStyle(UI_STYLE_ACTIVITY_ROOT);
    return ft.replace(id, fragment, TAG_LEAN_BACK_ACTIONS_FRAGMENT).commit();
}

From source file:com.distantfuture.castcompanionlibrary.lib.cast.player.VideoCastControllerActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cast_activity);
    loadAndSetupViews();/*www  .  j a  va  2  s  .c om*/
    mVolumeIncrement = CastUtils.getFloatFromPreference(this, VideoCastManager.PREFS_KEY_VOLUME_INCREMENT);
    try {
        mCastManager = VideoCastManager.getInstance(this);
    } catch (CastException e) {
        // logged already
    }

    setupActionBar();
    Bundle extras = getIntent().getExtras();
    if (null == extras) {
        finish();
        return;
    }

    FragmentManager fm = getFragmentManager();
    VideoCastControllerFragment mediaAuthFragment = (VideoCastControllerFragment) fm.findFragmentByTag("task");

    // if fragment is null, it means this is the first time, so create it
    if (mediaAuthFragment == null) {
        mediaAuthFragment = VideoCastControllerFragment.newInstance(extras);
        fm.beginTransaction().add(mediaAuthFragment, "task").commit();
        mListener = mediaAuthFragment;
        setOnVideoCastControllerChangedListener(mListener);
    } else {
        mListener = mediaAuthFragment;
        mListener.onConfigurationChanged();
    }
}

From source file:com.kennethmaffei.infinitegrid.HTTPCommManager.java

/**
 * Sets up a headless fragment that calls an asynctask to retrieve an image from a url
 * //from w  ww .j  ava2 s .  c o  m
 * @param url - the url which we will get data back from
 */
public void getURL(RecordDescriptor record) {
    if (activity == null)
        return;

    FragmentManager fm = activity.getFragmentManager();
    //Use the url as the fragment tag
    TaskFragment taskFragment = (TaskFragment) fm.findFragmentByTag(record.url);

    //If the Fragment is non-null, then it is currently being retained across a configuration change.
    if (taskFragment == null) {
        taskFragment = new TaskFragment();
        Bundle args = new Bundle();
        args.putInt("type", CALLTYPE.IMAGE.ordinal());
        args.putParcelable("record", record);
        taskFragment.setArguments(args);
        fm.beginTransaction().add(taskFragment, record.url).commit();
    }
}

From source file:com.tct.mail.ui.OnePaneController.java

@Override
protected void showConversation(Conversation conversation) {
    super.showConversation(conversation);
    // TS: tao.gan 2015-09-21 EMAIL FEATURE-559893 ADD_S
    //we change from ConversationListFragment to ConversationViewFragment, should let the toolbar show
    animateShow(null);/*from   ww  w.  jav a2 s  .c  o m*/
    // TS: tao.gan 2015-09-21 EMAIL FEATURE-559893 ADD_E
    mConversationListVisible = false;
    if (conversation == null) {
        transitionBackToConversationListMode();
        return;
    }
    disableCabMode();
    if (ConversationListContext.isSearchResult(mConvListContext)) {
        mViewMode.enterSearchResultsConversationMode();
    } else {
        mViewMode.enterConversationMode();
    }
    final FragmentManager fm = mActivity.getFragmentManager();
    final FragmentTransaction ft = fm.beginTransaction();
    // Switching to conversation view is an incongruous transition:
    // we are not replacing a fragment with another fragment as
    // usual. Instead, reveal the heretofore inert conversation
    // ViewPager and just remove the previously visible fragment
    // e.g. conversation list, or possibly label list?).
    final Fragment f = fm.findFragmentById(R.id.content_pane);
    // FragmentManager#findFragmentById can return fragments that are not added to the activity.
    // We want to make sure that we don't attempt to remove fragments that are not added to the
    // activity, as when the transaction is popped off, the FragmentManager will attempt to
    // readd the same fragment twice
    if (f != null && f.isAdded()) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.remove(f);
        ft.commitAllowingStateLoss();
        //TS: jin.dong 2015-8-28 EMAIL BUGFIX-1075110 MOD_S
        try {
            fm.executePendingTransactions();
        } catch (IllegalArgumentException e) {
            LogUtils.d(LOG_TAG, e, "IllegalArgumentException occurred");
        }
        //TS: jin.dong 2015-8-28 EMAIL BUGFIX-1075110 MOD_E
    }
    mPagerController.show(mAccount, mFolder, conversation, true /* changeVisibility */);
    onConversationVisibilityChanged(true);
    onConversationListVisibilityChanged(false);
}

From source file:android.support.v17.leanback.app.GuidedStepFragment.java

/**
 * Adds the specified GuidedStepFragment to the fragment stack, replacing any existing
 * GuidedStepFragments in the stack, and configuring the fragment-to-fragment custom
 * transitions.  A backstack entry is added, so the fragment will be dismissed when BACK key
 * is pressed./*from   w ww  .  ja  v a 2s .  com*/
 * <li>If current fragment on stack is GuidedStepFragment: assign {@link #UI_STYLE_REPLACE} and
 * {@link #onAddSharedElementTransition(FragmentTransaction, GuidedStepFragment)} will be called
 * to perform shared element transition between GuidedStepFragments.
 * <li>If current fragment on stack is not GuidedStepFragment: assign {@link #UI_STYLE_ENTRANCE}
 * <p>
 * Note: currently fragments added using this method must be created programmatically rather
 * than via XML.
 * @param fragmentManager The FragmentManager to be used in the transaction.
 * @param fragment The GuidedStepFragment to be inserted into the fragment stack.
 * @param id The id of container to add GuidedStepFragment, can be android.R.id.content.
 * @return The ID returned by the call FragmentTransaction.commit.
 */
public static int add(FragmentManager fragmentManager, GuidedStepFragment fragment, int id) {
    GuidedStepFragment current = getCurrentGuidedStepFragment(fragmentManager);
    boolean inGuidedStep = current != null;
    if (IS_FRAMEWORK_FRAGMENT && Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 23 && !inGuidedStep) {
        // workaround b/22631964 for framework fragment
        fragmentManager.beginTransaction().replace(id, new DummyFragment(), TAG_LEAN_BACK_ACTIONS_FRAGMENT)
                .commit();
    }
    FragmentTransaction ft = fragmentManager.beginTransaction();

    fragment.setUiStyle(inGuidedStep ? UI_STYLE_REPLACE : UI_STYLE_ENTRANCE);
    ft.addToBackStack(fragment.generateStackEntryName());
    if (current != null) {
        fragment.onAddSharedElementTransition(ft, current);
    }
    return ft.replace(id, fragment, TAG_LEAN_BACK_ACTIONS_FRAGMENT).commit();
}

From source file:com.patil.geobells.lite.MainActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    switch (position) {
    case 0:/*from  ww  w. ja v  a  2s . c  o m*/
        UpcomingRemindersFragment upcomingFragment = new UpcomingRemindersFragment();
        fragmentManager.beginTransaction().replace(R.id.container, upcomingFragment).commit();
        break;
    case 1:
        CompletedRemindersFragment completedFragment = new CompletedRemindersFragment();
        fragmentManager.beginTransaction().replace(R.id.container, completedFragment).commit();
        break;
    }
}

From source file:by.zatta.pilight.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    /*//from ww  w.j  av  a2s  . co  m
    * The action bar home/up should open or close the drawer. ActionBarDrawerToggle will take care of this.
    */
    publishList();
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    switch (item.getItemId()) {
    case R.id.menu_about:
        openDialogFragment(AboutDialog.newInstance());
        return true;
    case R.id.menu_settings:
        Fragment pref = fm.findFragmentByTag("prefs");
        if (pref == null) {
            ft.replace(R.id.fragment_main, new PrefFragment(), "prefs");
            fm.popBackStack();
            ft.addToBackStack(null);
            ft.commit();
        } else {
            ft.remove(fm.findFragmentByTag("prefs"));
            ft.commit();
            fm.popBackStack();
        }
        return true;
    default:
        break;
    }
    // Handle your other action bar items...
    return super.onOptionsItemSelected(item);
}