Java Code Examples for androidx.fragment.app.FragmentTransaction#replace()

The following examples show how to use androidx.fragment.app.FragmentTransaction#replace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void addFragment(FragmentActivity fragmentActivity, Fragment fragmentToAdd, String fragmentTag) {

        FragmentManager supportFragmentManager = fragmentActivity.getSupportFragmentManager();

        //Fragment activeFragment = UIService.getActiveFragment(fragmentActivity);
        FragmentTransaction fragmentTransaction = supportFragmentManager
                .beginTransaction();
        /*if (null != activeFragment) {
            fragmentTransaction.hide(activeFragment);
        }*/

        fragmentTransaction.replace(R.id.container, fragmentToAdd,
                fragmentTag);

        if (supportFragmentManager.getBackStackEntryCount() > 1) {
            supportFragmentManager.popBackStack();
        }
        fragmentTransaction.addToBackStack(fragmentTag);
        fragmentTransaction.commit();
        supportFragmentManager.executePendingTransactions();
        //Log.i(TAG, "BackStackEntryCount: " + supportFragmentManager.getBackStackEntryCount());
    }
 
Example 2
Source File: FragmentController.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
public void navigateToFragment(Fragment fragment, String tag, boolean addToBackStack, TransitionEffect animation, int containerResId) {

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if (animation != null && animation.isAnimation()) {
            transaction.setCustomAnimations(animation.enter, animation.exit, animation.popEnter, animation.popExit);
        }

        if (animation != null && animation.isTransition()) {
            transaction.setTransition(animation.transitionId);
        }

        transaction.replace(containerResId, fragment, fragment.getClass().getName());
        if (addToBackStack)
            transaction.addToBackStack(tag);

        try {
            transaction.commitAllowingStateLoss();
        } catch (Exception e) {
            logger.error("An error occurred while navigating to fragment.", e);
        }
    }
 
Example 3
Source File: SettingsParser.java    From InviZible with GNU General Public License v3.0 6 votes vote down vote up
private void readRules(String path, List<String> lines) {
    ArrayList<String> rules_file = new ArrayList<>();
    if (lines != null) {
        rules_file.addAll(lines);
    } else {
        rules_file.add("");
    }
    FragmentTransaction fTrans = settingsActivity.getSupportFragmentManager().beginTransaction();
    Bundle bundle = new Bundle();
    bundle.putStringArrayList("rules_file", rules_file);
    bundle.putString("path", path);
    ShowRulesRecycleFrag frag = new ShowRulesRecycleFrag();
    frag.setArguments(bundle);
    fTrans.replace(android.R.id.content, frag);
    fTrans.commit();
}
 
Example 4
Source File: OdysseyMainActivity.java    From odyssey with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onPlaylistSelected(String playlistTitle, long playlistID) {
    // Create fragment and give it an argument for the selected playlist
    PlaylistTracksFragment newFragment = PlaylistTracksFragment.newInstance(playlistTitle, playlistID);

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    // set enter / exit animation
    final int layoutDirection = getResources().getConfiguration().getLayoutDirection();
    newFragment.setEnterTransition(new Slide(GravityCompat.getAbsoluteGravity(GravityCompat.START, layoutDirection)));
    newFragment.setExitTransition(new Slide(GravityCompat.getAbsoluteGravity(GravityCompat.END, layoutDirection)));

    // Replace whatever is in the fragment_container view with this
    // fragment,
    // and add the transaction to the back stack so the user can navigate
    // back
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack("PlaylistTracksFragment");

    // Commit the transaction
    transaction.commit();
}
 
Example 5
Source File: FragmentVideoActivity.java    From GSYVideoPlayer with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    // 设置一个exit transition
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        getWindow().setEnterTransition(new Explode());
        getWindow().setExitTransition(new Explode());
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment);

    newFragment = new VideoFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frameLayout, newFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}
 
Example 6
Source File: PreferencesTorFragment.java    From InviZible with GNU General Public License v3.0 6 votes vote down vote up
private void openCountrySelectFragment(int nodesType, String keyStr) {
    if (!isAdded()) {
        return;
    }

    FragmentTransaction fTrans = getParentFragmentManager().beginTransaction();
    fTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    Fragment frg = new CountrySelectFragment();
    Bundle bndl = new Bundle();
    bndl.putInt("nodes_type", nodesType);
    bndl.putString("countries", val_tor.get(key_tor.indexOf(keyStr)));
    frg.setArguments(bndl);
    fTrans.replace(android.R.id.content, frg, "CountrySelectFragment");
    fTrans.addToBackStack("CountrySelectFragmentTag");
    fTrans.commit();
}
 
Example 7
Source File: SlidingPanelActivity.java    From mimi-reader with Apache License 2.0 6 votes vote down vote up
@Subscribe
public void openHistoryPage(OpenHistoryEvent event) {
    final FragmentManager fm = getSupportFragmentManager();
    final FragmentTransaction ft = fm.beginTransaction();

    final boolean saveBackStack;
    if (listFragment == boardsFragment) {
        saveBackStack = true;
    } else {
        saveBackStack = false;
    }

    HistoryFragment fragment = HistoryFragment.newInstance(event.watched);
    ft.replace(R.id.postitem_list, fragment, TAG_POST_LIST);

    if (saveBackStack) {
        ft.addToBackStack(null);
    }

    ft.commit();

    listFragment = fragment;
    panelLayout.openPane();
}
 
Example 8
Source File: MainActivity.java    From ui with Apache License 2.0 5 votes vote down vote up
@Override
public void onFragmentInteraction1(String Data) {

    //now change to the SecondFragment, pressing the back button should go to main fragment.
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    //remove firstfragment from the stack and replace it with two.
    transaction.replace(R.id.container, SecondFragment.newInstance(String.valueOf(num_two), Data));
    // and add the transaction to the back stack so the user can navigate back
    transaction.addToBackStack(null);

    // Commit the transaction
    transaction.commit();

    num_two++;
}
 
Example 9
Source File: MainActivity.java    From AndroidMaterialDialog with Apache License 2.0 5 votes vote down vote up
@Override
protected final void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG) == null) {
        fragment = (PreferenceFragment) Fragment
                .instantiate(this, PreferenceFragment.class.getName());
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment, fragment, FRAGMENT_TAG);
        transaction.commit();
    }

    initializeFloatingActionButton();
}
 
Example 10
Source File: LogViewActivity.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  dynamicTheme.onCreate(this);
  dynamicLanguage.onCreate(this);

  setContentView(R.layout.log_view_activity);
  logViewFragment = LogViewFragment.newInstance();
  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  transaction.replace(R.id.fragment_container, logViewFragment);
  transaction.commit();

  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
 
Example 11
Source File: MainActivity.java    From guanggoo-android with Apache License 2.0 5 votes vote down vote up
public static void addFragmentToStack(FragmentManager fm, Fragment fragment) {
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.fragment_container, fragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(sStackName);
    ft.commit();
}
 
Example 12
Source File: DConnectServiceListActivity.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
private void showServiceRemover() {
    FragmentManager mgr = getSupportFragmentManager();
    FragmentTransaction transaction = mgr.beginTransaction();
    Fragment fragment = new RemoverFragment();
    transaction.replace(R.id.fragment_container, fragment);
    transaction.commit();
}
 
Example 13
Source File: TreatmentsFragment.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
private void setFragment(Fragment selectedFragment) {
    FragmentTransaction ft = getChildFragmentManager().beginTransaction();
    ft.replace(R.id.treatments_fragment_container, selectedFragment); // f2_container is your FrameLayout container
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();
}
 
Example 14
Source File: KeyFobsActivity.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_key_fobs);

    ButterKnife.inject(this, this);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    keyFobActivityFragment = new KeyFobsActivityFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, keyFobActivityFragment);
    fragmentTransaction.commit();
    keyfobMode = MODE_SHOWING_KEYFOBS_LIST;

    keyFobBlinkingFragment = new KeyFobsActivityFindingDeviceFragment();

    // handle bluetooth adapter on/off state
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(bluetoothAdapterStateChangeListener, filter);
    bluetoothEnableBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            changeEnableBluetoothAdapterToConnecing();
            BluetoothAdapter.getDefaultAdapter().enable();
        }
    });

}
 
Example 15
Source File: MainActivity.java    From graphics-samples with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        DrawableTintingFragment fragment = new DrawableTintingFragment();
        transaction.replace(R.id.sample_content_fragment, fragment);
        transaction.commit();
    }
}
 
Example 16
Source File: BottinDetailsFragment.java    From ETSMobile-Android2 with Apache License 2.0 5 votes vote down vote up
private void showFragment(final Fragment fragment) {
		if (fragment == null)
			return;
// Begin the transaction
		FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
// Replace the contents of the container with the new fragment
		ft.replace(R.id.container, fragment);
// or ft.add(R.id.your_placeholder, new FooFragment());
// Complete the changes added above
		ft.commit();
	}
 
Example 17
Source File: AccessLogActivity.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * フラグメントを追加します.
 *
 * @param fragment 追加するフラグメント
 * @param isBackStack バックスタックに追加する場合はtrue、それ以外はfalse
 */
private void addFragment(Fragment fragment, boolean isBackStack) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(android.R.id.content, fragment, "container");
    if (isBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.commit();
}
 
Example 18
Source File: SlidingPanelActivity.java    From mimi-reader with Apache License 2.0 5 votes vote down vote up
@Override
    public void onBoardItemClick(ChanBoard board, boolean saveBackStack) {
        final Bundle arguments = new Bundle();
        arguments.putString(Extras.EXTRAS_BOARD_NAME, board.getName());
        arguments.putString(Extras.EXTRAS_BOARD_TITLE, board.getTitle());
        arguments.putBoolean(Extras.EXTRAS_OPTIONS_MENU_ENABLED, false);

        PostItemsListFragment fragment = new PostItemsListFragment();
        fragment.setArguments(arguments);

        final FragmentManager fm = getSupportFragmentManager();
        final FragmentTransaction ft = fm.beginTransaction();
        final Fragment catalogItemsFragment = fm.findFragmentByTag(TAG_POST_LIST);
        if (catalogItemsFragment != null) {
            ft.remove(catalogItemsFragment);
        }

        ft.replace(R.id.postitem_list, fragment, TAG_POST_LIST);

//        if (listFragment != null) {
//
//            ft.hide(listFragment);
//        }
//        ft.add(R.id.postitem_list, fragment, TAG_POST_LIST);

        if (saveBackStack) {
            ft.addToBackStack(null);
        }

        ft.commit();

        listFragment = fragment;
        setFabVisibility(fragment.showFab());
    }
 
Example 19
Source File: ThetaFeatureActivity.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * Fragment の遷移.
 * @param f Fragment
 */
private void moveFragment(final Fragment f) {
    FragmentTransaction t = getSupportFragmentManager().beginTransaction();
    t.setTransition(FragmentTransaction.TRANSIT_NONE);
    t.replace(android.R.id.content, f);
    t.addToBackStack(null);
    t.commit();

}
 
Example 20
Source File: HueFragment01.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * 指定されたアクセスポイントを指定して、次のフラグメントを開く.
 * @param accessPoint アクセスポイント
 */
private void moveNextFragment(final PHAccessPoint accessPoint) {
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.setCustomAnimations(R.anim.fragment_slide_right_enter, R.anim.fragment_slide_left_exit,
            R.anim.fragment_slide_left_enter, R.anim.fragment_slide_right_exit);
    transaction.replace(R.id.fragment_frame, HueFragment02.newInstance(accessPoint));
    transaction.commit();
}