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

The following examples show how to use androidx.fragment.app.FragmentManager#executePendingTransactions() . 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: AskPermission.java    From CommonUtils with Apache License 2.0 6 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (listener != null && code == requestCode) {
        for (int i = 0; i < permissions.length; i++) {
            if (grantResults[i] == PackageManager.PERMISSION_GRANTED)
                listener.permissionGranted(permissions[i]);
            else
                listener.permissionDenied(permissions[i]);
        }
    }

    try {
        if (getActivity() != null) {
            FragmentManager manager = getActivity().getSupportFragmentManager();
            manager.executePendingTransactions();
            manager.beginTransaction()
                    .remove(this)
                    .commitAllowingStateLoss();
        }
    } catch (IllegalStateException ex) {
        Log.w(TAG, ex);
    }
}
 
Example 3
Source File: ContactSelectionActivity.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();

    FragmentTransaction fragmentTransaction = supportFragmentManager
            .beginTransaction();
    fragmentTransaction.replace(R.id.layout_child_activity, fragmentToAdd,
            fragmentTag);

    if (supportFragmentManager.getBackStackEntryCount() > 1) {
        supportFragmentManager.popBackStack();
    }
    fragmentTransaction.addToBackStack(fragmentTag);
    fragmentTransaction.commitAllowingStateLoss();
    supportFragmentManager.executePendingTransactions();
}
 
Example 4
Source File: ReceiveResult.java    From FragmentMaster with Apache License 2.0 6 votes vote down vote up
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mResultView = (TextView) view.findViewById(R.id.resultView);
    view.findViewById(R.id.button).setOnClickListener(
            new OnClickListener() {
                @Override
                public void onClick(View v) {
                    startFragmentForResult(NumbersList.class, REQUEST_CODE);
                }
            });
    FragmentManager fragmentManager = getChildFragmentManager();
    if (fragmentManager.findFragmentByTag("TAG_CHILD") == null) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(R.id.childContainer, new Child(), "TAG_CHILD");
        ft.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }
}
 
Example 5
Source File: YouTubePlayerView.java    From android-inline-youtube-view with Apache License 2.0 6 votes vote down vote up
private YouTubeBaseFragment removeCurrentYouTubeFragment() {
    FragmentManager fragmentManager = fragment.getChildFragmentManager();
    Fragment youTubeFragment = fragmentManager.findFragmentByTag(TAG);
    YouTubeBaseFragment youTubeBaseFragment = null;
    if (youTubeFragment instanceof YouTubeBaseFragment) {
        youTubeBaseFragment = (YouTubeBaseFragment) youTubeFragment;
        View fragmentView = youTubeFragment.getView();
        ViewParent parentView = null != fragmentView ? fragmentView.getParent() : null;
        youTubeBaseFragment.release();
        fragmentManager.beginTransaction().remove(youTubeFragment).commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
        if (parentView instanceof View && ((View) parentView).getId() == R.id.youtubeFragmentContainer) {
            ((View) parentView).setId(0);
        }
    }
    return youTubeBaseFragment;
}
 
Example 6
Source File: FragmentInjectHelper.java    From FriendCircle with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
public static <F extends Fragment> F inject(@NonNull FragmentActivity target,
                                            @NonNull Class<F> fragClass,
                                            @Nullable Bundle arguments,
                                            @Nullable String tag) {
    if (!ActivityUtil.isAlive(target)) return null;
    if (TextUtils.isEmpty(tag)) {
        tag = fragClass.getName();
    }
    FragmentManager fragmentManager = target.getSupportFragmentManager();
    F result = (F) fragmentManager.findFragmentByTag(tag);
    if (result == null) {
        result = (F) fragmentManager.getFragmentFactory().instantiate(fragClass.getClassLoader(), fragClass.getName());
        result.setArguments(arguments);
        fragmentManager.beginTransaction()
                .add(result, tag)
                .commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }
    return result;
}
 
Example 7
Source File: PLauncher.java    From YImagePicker with Apache License 2.0 5 votes vote down vote up
private PRouterV4 getRouterFragmentV4(FragmentActivity activity) {
    PRouterV4 routerFragment = findRouterFragmentV4(activity);
    if (routerFragment == null) {
        routerFragment = PRouterV4.newInstance();
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        fragmentManager
                .beginTransaction()
                .add(routerFragment, TAG)
                .commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }
    return routerFragment;
}
 
Example 8
Source File: Themes.java    From FragmentMaster with Apache License 2.0 5 votes vote down vote up
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    FragmentManager fragmentManager = getChildFragmentManager();
    if (fragmentManager.findFragmentByTag("TAG_CHILD") == null) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(R.id.childContainer, new DarkThemeChildPage(), "TAG_CHILD");
        ft.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }
}
 
Example 9
Source File: ConversationActivity.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void addFragment(FragmentActivity fragmentActivity, Fragment fragmentToAdd, String fragmentTag) {
    if (fragmentActivity.isFinishing() || (fragmentActivity instanceof ConversationActivity && ((ConversationActivity) fragmentActivity).isActivityDestroyed)) {
        return;
    }
    if (Utils.hasJellyBeanMR1()) {
        if (fragmentActivity.isDestroyed()) {
            return;
        }
    }
    FragmentManager supportFragmentManager = fragmentActivity.getSupportFragmentManager();

    // Fragment activeFragment = UIService.getActiveFragment(fragmentActivity);
    FragmentTransaction fragmentTransaction = supportFragmentManager
            .beginTransaction();
    fragmentTransaction.replace(R.id.layout_child_activity, fragmentToAdd,
            fragmentTag);

    if (supportFragmentManager.getBackStackEntryCount() > 1
            && !ConversationUIService.MESSGAE_INFO_FRAGMENT.equalsIgnoreCase(fragmentTag) && !ConversationUIService.USER_PROFILE_FRAMENT.equalsIgnoreCase(fragmentTag)) {
        supportFragmentManager.popBackStackImmediate();
    }

    fragmentTransaction.addToBackStack(fragmentTag);
    fragmentTransaction.commitAllowingStateLoss();
    supportFragmentManager.executePendingTransactions();
    //Log.i(TAG, "BackStackEntryCount: " + supportFragmentManager.getBackStackEntryCount());
}
 
Example 10
Source File: AirMapView.java    From AirMapView with Apache License 2.0 5 votes vote down vote up
public void initialize(FragmentManager fragmentManager, AirMapInterface mapInterface) {
  if (mapInterface == null || fragmentManager == null) {
    throw new IllegalArgumentException("Either mapInterface or fragmentManager is null");
  }

  this.mapInterface = mapInterface;
  this.mapInterface.setOnMapLoadedListener(this);

  fragmentManager.beginTransaction()
      .replace(getId(), (Fragment) this.mapInterface)
      .commit();

  fragmentManager.executePendingTransactions();
}
 
Example 11
Source File: ScrollObservablesActivity.java    From UltimateRecyclerView with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_scroll_activity);
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    FragmentManager fm = getSupportFragmentManager();
    if (fm.findFragmentByTag(ViewPagerTabFragmentParentFragment.FRAGMENT_TAG) == null) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment, new ViewPagerTabFragmentParentFragment(),
                ViewPagerTabFragmentParentFragment.FRAGMENT_TAG);
        ft.commit();
        fm.executePendingTransactions();
    }
}
 
Example 12
Source File: Themes.java    From FragmentMaster with Apache License 2.0 5 votes vote down vote up
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    FragmentManager fragmentManager = getChildFragmentManager();
    if (fragmentManager.findFragmentByTag("TAG_CHILD") == null) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(R.id.childContainer, new LightThemeChildPage(), "TAG_CHILD");
        ft.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }
}
 
Example 13
Source File: EasyResult.java    From EasyPhotos with Apache License 2.0 5 votes vote down vote up
private HolderFragment getHolderFragment(FragmentManager fragmentManager) {
    HolderFragment holderFragment = findHolderFragment(fragmentManager);
    if (holderFragment == null) {
        holderFragment = new HolderFragment();
        fragmentManager
                .beginTransaction()
                .add(holderFragment, TAG)
                .commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }
    return holderFragment;
}
 
Example 14
Source File: AppCompatDialog.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void showNow(@NonNull FragmentManager manager, @Nullable String tag) {
    manager.executePendingTransactions();
    if (!isAdded()) {
        super.showNow(manager, tag);
    }
}
 
Example 15
Source File: FragmentHelper.java    From AndroidNavigation with MIT License 5 votes vote down vote up
public static void executePendingTransactionsSafe(@NonNull FragmentManager fragmentManager) {
    try {
        fragmentManager.executePendingTransactions();
    } catch (IllegalStateException e) {
        Log.wtf(TAG, e);
    }
}
 
Example 16
Source File: MainActivity.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 5 votes vote down vote up
private void popFragmentsIfNoPeripheralsConnected() {
    final int numConnectedPeripherals = BleManager.getInstance().getConnectedDevices().size();
    final boolean isLastConnectedPeripheral = numConnectedPeripherals == 0;

    if (isLastConnectedPeripheral && (!kAvoidPoppingFragmentsWhileOnDfu || !isIsDfuInProgress())) {
        Log.d(TAG, "No peripherals connected. Pop all fragments");
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fragmentManager.executePendingTransactions();
    }
}
 
Example 17
Source File: FragmentOperationsLifecycleTest.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Test
@UiThreadTest
public void fragmentInBackstack() {
    EmptyActivity activity = mActivityTestRule.getActivity();
    Fragment fragment1 = new Fragment();
    FragmentManager fm = activity.getSupportFragmentManager();
    fm.beginTransaction().add(R.id.fragment_container, fragment1, "tag").addToBackStack(null)
            .commit();
    fm.executePendingTransactions();
    CollectingObserver observer1 = observeAndCollectIn(fragment1);
    assertThat(observer1.getEventsAndReset(), is(asList(ON_CREATE, ON_START, ON_RESUME)));

    Fragment fragment2 = new Fragment();
    fm.beginTransaction().replace(R.id.fragment_container, fragment2).addToBackStack(null)
            .commit();
    fm.executePendingTransactions();

    CollectingObserver observer2 = observeAndCollectIn(fragment2);
    assertThat(observer1.getEventsAndReset(), is(asList(ON_PAUSE, ON_STOP)));
    assertThat(observer2.getEventsAndReset(), is(asList(ON_CREATE, ON_START, ON_RESUME)));

    assertThat(fm.popBackStackImmediate(), is(true));
    assertThat(observer1.getEventsAndReset(), is(asList(ON_START, ON_RESUME)));
    assertThat(observer2.getEventsAndReset(), is(asList(ON_PAUSE, ON_STOP, ON_DESTROY)));

    assertThat(fm.popBackStackImmediate(), is(true));
    assertThat(observer1.getEventsAndReset(), is(asList(ON_PAUSE, ON_STOP, ON_DESTROY)));
}
 
Example 18
Source File: SetupWizardActivity.java    From talkback with Apache License 2.0 4 votes vote down vote up
private void displayScreen(SetupScreen screen) {
  FragmentManager fragmentManager = getSupportFragmentManager();
  /* Hide the previous fragment. */
  if (currentScreenFragment != null) {
    /* In order to ensure that state information from the previous fragment has been saved, hide
     * the previous fragment with a separate fragment transaction than the one used to show the
     * new fragment. */
    fragmentManager
        .beginTransaction()
        .hide(currentScreenFragment)
        .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
        .commitAllowingStateLoss();
    fragmentManager.executePendingTransactions();
  }

  SetupWizardScreenFragment fragment =
      (SetupWizardScreenFragment) fragmentManager.findFragmentByTag(screen.name());
  FragmentTransaction transaction = fragmentManager.beginTransaction();

  transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
  if (fragment == null) {
    /* Create a new fragment. */
    fragment = createScreenFragment(screen);
    if (currentScreenFragment != null) {
      transaction.add(R.id.fragment_layout_container, fragment, screen.name());
    } else {
      transaction.replace(R.id.fragment_layout_container, fragment, screen.name());
    }
  } else {
    /* The desired fragment has been previously created, so show it. */
    transaction.show(fragment);
  }

  /* Update the view to reflect the new screen. */
  transaction.commitAllowingStateLoss();
  currentScreenFragment = fragment;
  currentSetupScreen = screen;
  setNavigationButtonText(currentScreenFragment);

  if (screenViewListener != null) {
    screenViewListener.onScreenShown(currentScreenFragment.getScreenName());
  }

  if (setupScreenListener != null) {
    setupScreenListener.onSetupScreenShown(currentSetupScreen);
  }
}
 
Example 19
Source File: AppcompatActivity.java    From ui with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_actionbar);

	//turn on up button
	getSupportActionBar().setDisplayHomeAsUpEnabled(true);

	
	FragmentManager fragmentManager = getSupportFragmentManager();
	if (savedInstanceState != null) {
		one = (PageFragment) fragmentManager.getFragment(savedInstanceState, "ONE");
		two = (PageFragment) fragmentManager.getFragment(savedInstanceState, "TWO");
		three = (PageFragment) fragmentManager.getFragment(savedInstanceState, "THREE");
		four = (PageFragment) fragmentManager.getFragment(savedInstanceState, "FOUR");
		five = (PageFragment) fragmentManager.getFragment(savedInstanceState, "FIVE");
		//since survived, need to clean up or I can't add them to the pagers adapter again.
		FragmentTransaction remove = fragmentManager.beginTransaction();
		remove.remove(one);
		remove.remove(two);
		remove.remove(three);
		remove.remove(four);
		remove.remove(five);
		if (!remove.isEmpty()) {
			remove.commit();
			fragmentManager.executePendingTransactions();
		}
	} else {
		one = PageFragment.create(1);
		two = PageFragment.create(2);
		three = PageFragment.create(3);
		four = PageFragment.create(4);
		five = PageFragment.create(5);
	}

	viewPager = (ViewPager) findViewById(R.id.pager);
	mPagerAdapter = new myFragmentPagerAdapter(5);
	viewPager.setAdapter(mPagerAdapter);
	//viewPager.setCurrentItem(2);
	//we need to know when a page has changed, so we can change/fix the next/previous/finish buttons
	viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
		@Override
		public void onPageSelected(int position) {
			supportInvalidateOptionsMenu();
		}
	});
}
 
Example 20
Source File: MainActivity.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 4 votes vote down vote up
public void startUpdate(@NonNull BlePeripheral blePeripheral, @NonNull ReleasesParser.BasicVersionInfo versionInfo) {
    dismissDfuProgressDialog();

    String message = getString(versionInfo.fileType == DfuService.TYPE_APPLICATION ? R.string.dfu_download_firmware_message : R.string.dfu_download_bootloader_message);
    mDfuProgressDialog = DfuProgressFragmentDialog.newInstance(blePeripheral.getDevice().getAddress(), message);
    FragmentManager fragmentManager = getSupportFragmentManager();
    mDfuProgressDialog.show(fragmentManager, null);
    fragmentManager.executePendingTransactions();

    mDfuProgressDialog.setIndeterminate(true);
    mDfuProgressDialog.setOnCancelListener(dialog -> {
        mDfuViewModel.cancelInstall();
        dfuFinished();
    });

    mIsDfuInProgress = true;
    mDfuViewModel.downloadAndInstall(this, blePeripheral, versionInfo, new DfuUpdater.DownloadStateListener() {
        @Override
        public void onDownloadStarted(int type) {
            mDfuProgressDialog.setIndeterminate(true);
            mDfuProgressDialog.setMessage(type == DfuUpdater.kDownloadOperation_Software_Hex ? R.string.dfu_download_hex_message : R.string.dfu_download_init_message);
        }

        @Override
        public void onDownloadProgress(int percent) {
            if (mDfuProgressDialog != null) {       // Check null (Google crash logs)
                mDfuProgressDialog.setIndeterminate(false);
                mDfuProgressDialog.setProgress(percent);
            }
        }

        @Override
        public void onDownloadFailed() {
            dismissDfuProgressDialog();

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle(R.string.dialog_error).setMessage(R.string.dfu_download_error_message)
                    .setPositiveButton(android.R.string.ok, null)
                    .show();
        }
    });
}