Java Code Examples for androidx.fragment.app.FragmentManager#beginTransaction()

The following examples show how to use androidx.fragment.app.FragmentManager#beginTransaction() . 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: StageActivity.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
public void refreshTopScene() {
    int index = mSceneTagList.size() - 1;
    if (index < 0) {
        return;
    }
    String tag = mSceneTagList.get(index);

    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(tag);

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.detach(fragment);
    transaction.attach(fragment);
    transaction.commitAllowingStateLoss();
    onTransactScene();
}
 
Example 2
Source File: LicensesFragment.java    From mimi-reader with Apache License 2.0 6 votes vote down vote up
/**
 * Builds and displays a licenses fragment for you. Requires "/res/raw/licenses.html" and
 * "/res/layout/licenses_fragment.xml" to be present.
 *
 * @param fm A fragment manager instance used to display this LicensesFragment.
 */
public static void displayLicensesFragment(FragmentManager fm, @RawRes int htmlResToShow, String title) {
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag(FRAGMENT_TAG);
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    final DialogFragment newFragment;
    if (TextUtils.isEmpty(title)) {
        newFragment = LicensesFragment.newInstance(htmlResToShow);
    } else {
        newFragment = LicensesFragment.newInstance(htmlResToShow, title);
    }
    newFragment.show(ft, FRAGMENT_TAG);
}
 
Example 3
Source File: FragmentHelper.java    From AndroidNavigation with MIT License 6 votes vote down vote up
public static void addFragmentToBackStack(@NonNull FragmentManager fragmentManager, int containerId, @NonNull AwesomeFragment fragment, @NonNull PresentAnimation animation) {
    if (fragmentManager.isDestroyed()) {
        return;
    }

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.setReorderingAllowed(true);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    AwesomeFragment topFragment = (AwesomeFragment) fragmentManager.findFragmentById(containerId);
    if (topFragment != null && topFragment.isAdded()) {
        topFragment.setAnimation(animation);
        transaction.setMaxLifecycle(topFragment, Lifecycle.State.STARTED);
        transaction.hide(topFragment);
    }
    fragment.setAnimation(animation);

    transaction.add(containerId, fragment, fragment.getSceneId());
    transaction.addToBackStack(fragment.getSceneId());
    transaction.commit();
    executePendingTransactionsSafe(fragmentManager);
}
 
Example 4
Source File: SlidingPanelActivity.java    From mimi-reader with Apache License 2.0 6 votes vote down vote up
@Override
public void onBackPressed() {
    if (panelLayout.isOpen()) {
        if (listFragment instanceof HistoryFragment) {
            final FragmentManager fm = getSupportFragmentManager();
            final FragmentTransaction ft = fm.beginTransaction();

            ft.remove(listFragment).commit();
        }
        listFragment = boardsFragment;
        super.onBackPressed();

        listFragment.initMenu();
    } else {
        panelLayout.openPane();
    }
}
 
Example 5
Source File: FleetConnectivityActivity.java    From here-android-sdk-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Pushes the @{link JobDetailsFragment} on the stack.
 *
 * @param jobId ID of the job of which the details should be displayed.
 */
@Override
public void showJobDetails(String jobId) {
    FragmentManager fm = getSupportFragmentManager();
    if (fm.getBackStackEntryCount() > 0 && JobDetailsFragment.TAG.equals(fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 1).getName())) {
        fm.popBackStack();
    }
    FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(R.anim.right_enter, R.anim.right_exit, R.anim.right_enter, R.anim.right_exit);
    ft.add(R.id.container, JobDetailsFragment.newInstance(jobId), JobDetailsFragment.TAG);
    ft.addToBackStack(JobDetailsFragment.TAG);
    ft.commit();
}
 
Example 6
Source File: ActivityUtils.java    From PopularMovies with MIT License 5 votes vote down vote up
/**
 * The {@code fragment} is added to the container view with id {@code frameId}. The operation is
 * performed by the {@code fragmentManager}.
 */
public static void replaceFragmentInActivity(@NonNull FragmentManager fragmentManager,
                                             @NonNull Fragment fragment, int frameId) {
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(frameId, fragment);
    transaction.commit();
}
 
Example 7
Source File: ActivityView.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private void init() {
    Bundle args = new Bundle();

    long account = getIntent().getLongExtra("account", -1);

    FragmentBase fragment;
    switch (startup) {
        case "accounts":
            fragment = new FragmentAccounts();
            args.putBoolean("settings", false);
            break;
        case "folders":
            fragment = new FragmentFolders();
            args.putLong("account", account);
            break;
        case "primary":
            fragment = new FragmentFolders();
            if (account < 0)
                args.putBoolean("primary", true);
            else
                args.putLong("account", account);
            break;
        default:
            fragment = new FragmentMessages();
    }

    fragment.setArguments(args);

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    for (Fragment existing : fm.getFragments())
        fragmentTransaction.remove(existing);
    fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("unified");
    fragmentTransaction.commit();
}
 
Example 8
Source File: ViewUtils.java    From SmartFlasher with GNU General Public License v3.0 5 votes vote down vote up
public static void dismissDialog(FragmentManager manager) {
    FragmentTransaction ft = manager.beginTransaction();
    Fragment fragment = manager.findFragmentByTag("dialog");
    if (fragment != null) {
        ft.remove(fragment).commit();
    }
}
 
Example 9
Source File: NavigationActivity.java    From SmartPack-Kernel-Manager with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void finish() {
    super.finish();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    for (int id : mActualFragments.keySet()) {
        Fragment fragment = fragmentManager.findFragmentByTag(id + "_key");
        if (fragment != null) {
            fragmentTransaction.remove(fragment);
        }
    }
    fragmentTransaction.commitAllowingStateLoss();
    RootUtils.closeSU();
}
 
Example 10
Source File: KeyFobsActivity.java    From EFRConnect-android with Apache License 2.0 5 votes vote down vote up
private void swapFragment(Fragment fragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}
 
Example 11
Source File: NavigationActivity.java    From MTweaks-KernelAdiutorMOD with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void finish() {
    super.finish();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    for (int id : mActualFragments.keySet()) {
        Fragment fragment = fragmentManager.findFragmentByTag(id + "_key");
        if (fragment != null) {
            fragmentTransaction.remove(fragment);
        }
    }
    fragmentTransaction.commitAllowingStateLoss();
    RootUtils.closeSU();
}
 
Example 12
Source File: HueFragment02.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
/**
 * 最初のフラグメントに移動します.
 */
private void moveFirstFragment() {
    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, new HueFragment01());
    transaction.commit();
}
 
Example 13
Source File: HueFragment02.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void onClick(final View v) {
    if (mHueStatus == HueManager.HueState.AUTHENTICATE_SUCCESS) {
        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, HueFragment03.newInstance(mAccessPoint));
        transaction.commit();
    } else {
        mButton.setVisibility(View.INVISIBLE);
        startAuthenticate();
    }
}
 
Example 14
Source File: ActivityUtils.java    From simple-stack with Apache License 2.0 5 votes vote down vote up
/**
 * The {@code fragment} is added to the container view with id {@code frameId}. The operation is
 * performed by the {@code fragmentManager}.
 */
public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager, @NonNull Fragment fragment, int frameId) {
    checkNotNull(fragmentManager);
    checkNotNull(fragment);
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.add(frameId, fragment);
    transaction.commit();
}
 
Example 15
Source File: MainActivity.java    From FChat with MIT License 5 votes vote down vote up
private void initComponent() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    ConversationListFragment ctf = new ConversationListFragment();
    //icf.setRetainInstance(true);
    fragmentTransaction.add(R.id.main_container, ctf, Constants.TAG_CHAT_HISTORY);
    fragmentTransaction.commit();

}
 
Example 16
Source File: MyActivity.java    From googleads-ima-android with Apache License 2.0 5 votes vote down vote up
private void orientAppUi() {
  int orientation = getResources().getConfiguration().orientation;
  boolean isLandscape = (orientation == Configuration.ORIENTATION_LANDSCAPE);
  // Hide the non-video content when in landscape so the video is as large as possible.
  FragmentManager fragmentManager = getSupportFragmentManager();
  VideoFragment videoFragment =
      (VideoFragment) fragmentManager.findFragmentByTag(VIDEO_EXAMPLE_FRAGMENT_TAG);

  Fragment videoListFragment = fragmentManager.findFragmentByTag(VIDEO_PLAYLIST_FRAGMENT_TAG);

  if (videoFragment != null) {
    // If the video playlist is onscreen (tablets) then hide that fragment.
    if (videoListFragment != null) {
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      if (isLandscape) {
        fragmentTransaction.hide(videoListFragment);
      } else {
        fragmentTransaction.show(videoListFragment);
      }
      fragmentTransaction.commit();
    }
    videoFragment.makeFullscreen(isLandscape);
    if (isLandscape) {
      hideStatusBar();
    } else {
      showStatusBar();
    }
  } else {
    // If returning to the list from a fullscreen video, check if the video
    // list fragment exists and is hidden. If so, show it.
    if (videoListFragment != null && videoListFragment.isHidden()) {
      fragmentManager.beginTransaction().show(videoListFragment).commit();
      showStatusBar();
    }
  }
}
 
Example 17
Source File: IntentChooserFragment.java    From candybar with Apache License 2.0 5 votes vote down vote up
public static void showIntentChooserDialog(@NonNull FragmentManager fm, int type) {
    FragmentTransaction ft = fm.beginTransaction();
    Fragment prev = fm.findFragmentByTag(TAG);
    if (prev != null) {
        ft.remove(prev);
    }

    try {
        DialogFragment dialog = IntentChooserFragment.newInstance(type);
        dialog.show(ft, TAG);
    } catch (IllegalArgumentException | IllegalStateException ignored) {
    }
}
 
Example 18
Source File: PhotoItemSelectedDialog.java    From PictureSelector with Apache License 2.0 4 votes vote down vote up
@Override
public void show(FragmentManager manager, String tag) {
    FragmentTransaction ft = manager.beginTransaction();
    ft.add(this, tag);
    ft.commitAllowingStateLoss();
}
 
Example 19
Source File: ViewUtils.java    From MTweaks-KernelAdiutorMOD with GNU General Public License v3.0 4 votes vote down vote up
public static void showDialog(FragmentManager manager, DialogFragment fragment) {
    FragmentTransaction ft = manager.beginTransaction();
    fragment.show(ft, "dialog");
}
 
Example 20
Source File: MessageActivity.java    From tindroid with Apache License 2.0 4 votes vote down vote up
void showFragment(String tag, Bundle args, boolean addToBackstack) {
    if (isFinishing() || isDestroyed()) {
        return;
    }

    FragmentManager fm = getSupportFragmentManager();

    Fragment fragment = fm.findFragmentByTag(tag);
    if (fragment == null) {
        switch (tag) {
            case FRAGMENT_MESSAGES:
                fragment = new MessagesFragment();
                break;
            case FRAGMENT_INFO:
                fragment = new TopicInfoFragment();
                break;
            case FRAGMENT_PERMISSIONS:
                fragment = new TopicPermissionsFragment();
                break;
            case FRAGMENT_EDIT_MEMBERS:
                fragment = new EditMembersFragment();
                break;
            case FRAGMENT_VIEW_IMAGE:
                fragment = new ImageViewFragment();
                break;
            case FRAGMENT_FILE_PREVIEW:
                fragment = new FilePreviewFragment();
                break;
            case FRAGMENT_INVALID:
                fragment = new InvalidTopicFragment();
                break;
        }
    } else if (args == null) {
        // Retain old arguments.
        args = fragment.getArguments();
    }
    if (fragment == null) {
        throw new NullPointerException();
    }

    args = args != null ? args : new Bundle();
    args.putString("topic", mTopicName);
    args.putString(MessagesFragment.MESSAGE_TO_SEND, mMessageText);
    if (fragment.getArguments() != null) {
        fragment.getArguments().putAll(args);
    } else {
        fragment.setArguments(args);
    }

    FragmentTransaction trx = fm.beginTransaction();
    if (!fragment.isAdded()) {
        trx = trx.replace(R.id.contentFragment, fragment, tag)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    } else if (!fragment.isVisible()) {
        trx = trx.show(fragment);
    } else {
        addToBackstack = false;
    }

    if (FRAGMENT_MESSAGES.equals(tag)) {
        trx.setPrimaryNavigationFragment(fragment);
    }

    if (addToBackstack) {
        trx.addToBackStack(tag);
    }
    if (!trx.isEmpty()) {
        trx.commit();
    }
}