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.tozny.e3db.android.DefaultKeyAuthenticator.java

@RequiresApi(api = Build.VERSION_CODES.M)
@Override/*ww  w  .  ja va  2 s. c om*/
public void authenticateWithLockScreen(AuthenticateHandler cont) {
    DeviceCredentialsFragment f = new DeviceCredentialsFragment(cont, title,
            (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE));
    FragmentManager fragmentManager = activity.getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(f, "device_credentials_fragment");
    fragmentTransaction.commit();
}

From source file:de.sourcestream.movieDB.controller.MovieDetailsInfo.java

/**
 * Callback method to be invoked when an item in this AdapterView has been clicked.
 *
 * @param parent   The AdapterView where the click happened.
 * @param view     The view within the AdapterView that was clicked (this will be a view provided by the adapter)
 * @param position The position of the view in the adapter.
 * @param id       The row id of the item that was clicked.
 *///www.  j  a va2 s.c  o m
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    activity.setRestoreMovieDetailsAdapterState(true);
    activity.setRestoreMovieDetailsState(false);
    if (activity.getMovieDetailsSimFragment() != null
            && activity.getLastVisitedSimMovie() == similarList.get(position).getId()
            && activity.getMovieDetailsSimFragment().getTimeOut() == 0) {
        // Old movie details retrieve info and re-init component else crash
        activity.getMovieDetailsSimFragment().onSaveInstanceState(new Bundle());
        Bundle bundle = new Bundle();
        bundle.putInt("id", similarList.get(position).getId());
        Bundle save = activity.getMovieDetailsSimFragment().getSave();
        // Re-init movie details and set save information
        movieDetails = new MovieDetails();
        movieDetails.setTimeOut(0);
        movieDetails.setSave(save);
        movieDetails.setArguments(bundle);
    } else
        movieDetails = new MovieDetails();

    activity.setLastVisitedSimMovie(similarList.get(position).getId());
    activity.getMovieDetailsFragment().setAddToBackStack(true);
    activity.getMovieDetailsFragment().onSaveInstanceState(new Bundle());
    if (activity.getSearchViewCount())
        activity.incSearchMovieDetails();

    activity.setMovieDetailsFragment(null);
    activity.setSaveInMovieDetailsSimFragment(true);
    movieDetails.setTitle(similarList.get(position).getTitle());
    FragmentManager manager = getActivity().getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    Bundle bundle = new Bundle();
    bundle.putInt("id", similarList.get(position).getId());
    movieDetails.setArguments(bundle);
    transaction.replace(R.id.frame_container, movieDetails);
    // add the current transaction to the back stack:
    transaction.addToBackStack("similarDetails");
    transaction.commit();

}

From source file:com.smart.taxi.activities.ContainerActivity.java

public void selectItem(int position) {
    Fragment fragment;/*  w ww  . j  a  va2 s.  co  m*/
    String tag = null;
    if (lastIndex == position && lastFragment instanceof TripRequestFragment
            && SplashActivity.isTripRequested) {
        if (tripRequestRef != null) {
            tripRequestRef.reset(null);
        }
        return;
    } else {
        if (position != 5)
            lastIndex = position;
        switch (position) {
        case 0:
            if (SplashActivity.isTripRequested && SplashActivity.getTripNewDetails() != null) {
                fragment = new TripRequestFragment();
                tag = TripRequestFragment.TAG;
            } else {
                fragment = new FindARideFragment();
                tag = FindARideFragment.TAG;
            }
            break;
        case 1:
            fragment = new CurrentTripFragment();
            tag = CurrentTripFragment.TAG;
            break;
        case 2:
            fragment = new TripHistoryFragment();
            tag = TripHistoryFragment.TAG;
            break;

        case 3:
            fragment = new ProfileFragment();
            tag = ProfileFragment.TAG;
            break;

        case 4:
            if (SplashActivity.loggedInUser.isCorporateUser()) {
                lastIndex = 0;
                Intent intent = new Intent(this, BarcodeActivity.class);
                startActivity(intent);
                return;
            } else {
                fragment = new PaymentOptionsFragment();
                tag = PaymentOptionsFragment.TAG;
                break;
            }
        case 5:
            fragment = new ChangeTipFragment();
            tag = ChangeTipFragment.TAG;
            break;

        case 6:
            SplashActivity.doLogoutAction = true;
            SplashActivity.logout();
            lastIndex = 0;
            lastFragment = null;
            finish();
            return;

        default:
            mDrawerLayout.closeDrawer(mDrawer);
            return;
        }
        lastFragment = fragment;

        FragmentManager manager = getFragmentManager();

        manager.beginTransaction().setTransition(FragmentTransaction.TRANSIT_ENTER_MASK)
                .replace(R.id.content_frame, fragment).addToBackStack(tag).commit();
        mDrawerList.setItemChecked(position, true);
        setTitle(mNavigationLabels[position]);
        mDrawerLayout.closeDrawer(mDrawer);
    }

    // update the main content by replacing fragments
    /*Fragment fragment = new PlanetFragment();
    Bundle args = new Bundle();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);
            
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
            
    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mNavigationLabels[position]);
    mDrawerLayout.closeDrawer(mDrawerList);*/
}

From source file:com.github.cpmproto.categorystepfragment.base.GuidedStepListFragment.java

/**
 * Adds the specified GuidedStepListFragment to the fragment stack, replacing any existing
 * GuidedStepListFragments 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  w  w .  ja  v  a 2s. c  o m
 * <li>If current fragment on stack is GuidedStepListFragment: assign {@link #UI_STYLE_REPLACE} and
 * {@link #onAddSharedElementTransition(FragmentTransaction, GuidedStepListFragment)} will be called
 * to perform shared element transition between GuidedStepListFragments.
 * <li>If current fragment on stack is not GuidedStepListFragment: 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 GuidedStepListFragment to be inserted into the fragment stack.
 * @param id              The id of container to add GuidedStepListFragment, can be android.R.id.content.
 * @return The ID returned by the call FragmentTransaction.commit.
 */
public static int add(FragmentManager fragmentManager, GuidedStepListFragment fragment, int id) {
    GuidedStepListFragment current = getCurrentGuidedStepListFragment(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:systems.soapbox.ombuds.client.ui.AddressBookActivity.java

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

    setContentView(R.layout.address_book_content);
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    super.setAnimateOnPause(true);

    final FragmentManager fragmentManager = getFragmentManager();

    walletAddressesFragment = (WalletAddressesFragment) fragmentManager.findFragmentByTag(TAG_LEFT);
    sendingAddressesFragment = (SendingAddressesFragment) fragmentManager.findFragmentByTag(TAG_RIGHT);

    final FragmentTransaction removal = fragmentManager.beginTransaction();

    if (walletAddressesFragment == null)
        walletAddressesFragment = new WalletAddressesFragment();
    else/*from w  w w .  ja  v a2s  . c om*/
        removal.remove(walletAddressesFragment);

    if (sendingAddressesFragment == null)
        sendingAddressesFragment = new SendingAddressesFragment();
    else
        removal.remove(sendingAddressesFragment);

    if (!removal.isEmpty()) {
        removal.commit();
        fragmentManager.executePendingTransactions();
    }

    final ViewPager pager = (ViewPager) findViewById(R.id.address_book_pager);
    if (pager != null) {
        pager.setAdapter(
                new TwoFragmentAdapter(fragmentManager, walletAddressesFragment, sendingAddressesFragment));

        final ViewPagerTabs pagerTabs = (ViewPagerTabs) findViewById(R.id.address_book_pager_tabs);
        pagerTabs.addTabLabels(R.string.address_book_list_receiving_title,
                R.string.address_book_list_sending_title);

        pager.setOnPageChangeListener(pagerTabs);
        final int position = 1;
        pager.setCurrentItem(position);
        pager.setPageMargin(2);
        pager.setPageMarginDrawable(R.color.bg_less_bright);

        pagerTabs.onPageSelected(position);
        pagerTabs.onPageScrolled(position, 0, 0);
    } else {
        fragmentManager.beginTransaction()
                .add(R.id.wallet_addresses_fragment, walletAddressesFragment, TAG_LEFT)
                .add(R.id.sending_addresses_fragment, sendingAddressesFragment, TAG_RIGHT).commit();
    }

    updateFragments();
}

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

@Override
protected void showConversation(Conversation conversation, boolean inLoaderCallbacks) {
    super.showConversation(conversation, inLoaderCallbacks);
    mConversationListVisible = false;//from   www. j  a v  a2  s .co m
    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();
        fm.executePendingTransactions();
    }
    mPagerController.show(mAccount, mFolder, conversation, true /* changeVisibility */);
    onConversationVisibilityChanged(true);
    onConversationListVisibilityChanged(false);
}

From source file:biz.wiz.android.wallet.ui.AddressBookActivity.java

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

    setContentView(R.layout.address_book_content);

    final ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    final FragmentManager fragmentManager = getFragmentManager();

    walletAddressesFragment = (WalletAddressesFragment) fragmentManager.findFragmentByTag(TAG_LEFT);
    sendingAddressesFragment = (SendingAddressesFragment) fragmentManager.findFragmentByTag(TAG_RIGHT);

    final FragmentTransaction removal = fragmentManager.beginTransaction();

    if (walletAddressesFragment == null)
        walletAddressesFragment = new WalletAddressesFragment();
    else/* w w w. j  av  a  2 s  . co m*/
        removal.remove(walletAddressesFragment);

    if (sendingAddressesFragment == null)
        sendingAddressesFragment = new SendingAddressesFragment();
    else
        removal.remove(sendingAddressesFragment);

    if (!removal.isEmpty()) {
        removal.commit();
        fragmentManager.executePendingTransactions();
    }

    final ViewPager pager = (ViewPager) findViewById(R.id.address_book_pager);
    if (pager != null) {
        pager.setAdapter(
                new TwoFragmentAdapter(fragmentManager, walletAddressesFragment, sendingAddressesFragment));

        final ViewPagerTabs pagerTabs = (ViewPagerTabs) findViewById(R.id.address_book_pager_tabs);
        pagerTabs.addTabLabels(R.string.address_book_list_receiving_title,
                R.string.address_book_list_sending_title);

        pager.setOnPageChangeListener(pagerTabs);
        final int position = 1;
        pager.setCurrentItem(position);
        pager.setPageMargin(2);
        pager.setPageMarginDrawable(R.color.bg_less_bright);

        pagerTabs.onPageSelected(position);
        pagerTabs.onPageScrolled(position, 0, 0);
    } else {
        fragmentManager.beginTransaction()
                .add(R.id.wallet_addresses_fragment, walletAddressesFragment, TAG_LEFT)
                .add(R.id.sending_addresses_fragment, sendingAddressesFragment, TAG_RIGHT).commit();
    }

    updateFragments();
}

From source file:com.jainbooks.activitys.DashboardActivity.java

public void selectItem(int position) {
    Fragment fragment = new EBookStoreFragment();

    if (position == 0) {

        boolean isNetworkConnection = Utils.isNetworkAvailable(activity);
        Store store = ((App) getApplication()).getStore();
        if (store != null || isNetworkConnection) {
            fragment = new EBookStoreFragment();

        } else {//from   w  ww.  j  a v  a2  s .c o m
            fragment = new EBookNetworkHandlerFragment();

        }
    } else if (position == 1) {
        fragment = new EBookMyLibraryFragment();

        /*   Uri uri = Uri.parse("file:///android_asset/and.pdf");
           Intent intent = new Intent(this,MuPDFActivity.class);
           intent.setAction(Intent.ACTION_VIEW);
           intent.setData(uri);
           startActivity(intent);
           return;*/
    } else if (position == 2) {
        fragment = new AccountFragment();
    }

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mFragmentTitlesTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);

}

From source file:it.gmariotti.cardslib.demo.MainActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo_activity_main);

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    _initMenu();/*from   w ww .ja  v  a  2s  . co  m*/
    mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
    mDrawer.setDrawerListener(mDrawerToggle);

    // ---------------------------------------------------------------
    // ...
    String base64EncodedPublicKey = IabUtil.key;

    // compute your public key and store it in base64EncodedPublicKey
    mHelper = new IabHelper(this, base64EncodedPublicKey);
    mHelper.enableDebugLogging(true);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                // Oh noes, there was a problem.
                Log.d(TAG, "Problem setting up In-app Billing: " + result);
            }

            // Have we been disposed of in the meantime? If so, quit.
            if (mHelper == null)
                return;

            // Hooray, IAB is fully set up!
            IabUtil.getInstance().retrieveData(mHelper);
        }
    });

    //-----------------------------------------------------------------
    //BaseFragment baseFragment = null;
    if (savedInstanceState != null) {
        mSelectedFragment = savedInstanceState.getInt(BUNDLE_SELECTEDFRAGMENT);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (fragmentManager.findFragmentById(R.id.fragment_main) == null)
            mBaseFragment = selectFragment(mSelectedFragment);
        //if (mBaseFragment==null)
        //    mBaseFragment = selectFragment(mSelectedFragment);
    } else {
        mBaseFragment = new HeaderFragment();
        openFragment(mBaseFragment);
    }

    //-----------------------------------------------------------------
}

From source file:io.radio.streamer.MainActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments
    Fragment fragment = new MenuFragment();
    Bundle args = new Bundle();
    args.putString(MenuFragment.ARG_PAGE_ID, mDrawerMenu[position]);
    fragment.setArguments(args);//www.ja  va 2s  .com

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mDrawerMenu[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}