Java Code Examples for android.support.v4.app.FragmentTransaction#commitAllowingStateLoss()

The following examples show how to use android.support.v4.app.FragmentTransaction#commitAllowingStateLoss() . 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: PhotoSelectActivity.java    From star-zone-android with Apache License 2.0 6 votes vote down vote up
private void changeFragment(BaseFragment from, BaseFragment to, boolean needAnima) {
    if (to == null) return;
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    if (needAnima) {
        setUpAnima(transaction, from, to);
    }
    if (!to.isAdded()) {
        transaction.add(R.id.photo_select_content, to);
    } else {
        if (from != null) {
            transaction.hide(from).show(to);
        }
    }
    currentFragment = to;
    transaction.commitAllowingStateLoss();
}
 
Example 2
Source File: WebViewCarFragment.java    From carstream-android-auto with Apache License 2.0 6 votes vote down vote up
private void showBookmarksScreen() {
    if (isAdded()) {
        webView.setVisibility(View.INVISIBLE);
        toolbar.setVisibility(View.GONE);
        FragmentManager childFragmentManager = getChildFragmentManager();
        BookmarksFragment bookmarksFragment = (BookmarksFragment) childFragmentManager.findFragmentByTag(BOOKMARKS_FRAGMENT_TAG);
        if (bookmarksFragment == null) {
            bookmarksFragment = new BookmarksFragment();
        }
        FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.overlay_container, bookmarksFragment, BOOKMARKS_FRAGMENT_TAG);
        fragmentTransaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
        fragmentTransaction.commitAllowingStateLoss();
    }

}
 
Example 3
Source File: PaymentResultActivity.java    From px-android with MIT License 6 votes vote down vote up
private void loadRemedies(@NonNull final PaymentModel paymentModel,
    @NonNull final PaymentResultViewModel model) {
    final FragmentManager fragmentManager = getSupportFragmentManager();
    if (fragmentManager != null) {
        remediesFragment = (RemediesFragment) fragmentManager.findFragmentByTag(RemediesFragment.TAG);
        payButtonFragment = (PayButtonFragment) fragmentManager.findFragmentByTag(PayButtonFragment.TAG);

        if (remediesFragment == null || payButtonFragment == null) {
            final FragmentTransaction transaction = fragmentManager.beginTransaction();
            if (remediesFragment == null) {
                remediesFragment = RemediesFragment.newInstance(paymentModel, model.remediesModel);
                transaction.replace(R.id.remedies, remediesFragment, RemediesFragment.TAG);
            }
            if (payButtonFragment == null) {
                payButtonFragment = new PayButtonFragment();
                transaction.replace(R.id.pay_button, payButtonFragment, PayButtonFragment.TAG);
            }
            transaction.commitAllowingStateLoss();
        }

        footer.setVisibility(View.VISIBLE);
        footer.init(model.footerModel, remediesFragment);
        findViewById(R.id.remedies).setVisibility(View.VISIBLE);
    }
}
 
Example 4
Source File: GenericChannelActivity.java    From buddycloud-android with Apache License 2.0 6 votes vote down vote up
@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_generic_channels);
     
String adapterName = getIntent().getStringExtra(ADAPTER_NAME);
GenericChannelAdapter adapter = createAdapter(adapterName);
if (adapter != null) {
	ActionbarUtil.showActionBarwithBack(this, adapter.getTitle(getApplicationContext()));
}

      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
final GenericSelectableChannelsFragment frag = new GenericSelectableChannelsFragment();
frag.setAdapter(adapter);
transaction.replace(R.id.contentFrame, frag);
transaction.commitAllowingStateLoss();
  }
 
Example 5
Source File: BottomSheetFragmentDelegate.java    From bottomsheet with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void dismissInternal(boolean allowStateLoss) {
    if (dismissed) {
        return;
    }
    dismissed = true;
    shownByMe = false;
    if (bottomSheetLayout != null) {
        bottomSheetLayout.dismissSheet();
        bottomSheetLayout = null;
    }
    viewDestroyed = true;
    if (backStackId >= 0) {
        fragment.getFragmentManager().popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        backStackId = -1;
    } else {
        FragmentTransaction ft = fragment.getFragmentManager().beginTransaction();
        ft.remove(fragment);
        if (allowStateLoss) {
            ft.commitAllowingStateLoss();
        } else {
            ft.commit();
        }
    }
}
 
Example 6
Source File: OHCompactAlertDialogFragment.java    From oneHookLibraryAndroid with Apache License 2.0 6 votes vote down vote up
private OHCompactAlertDialogFragment show(FragmentManager manager, final String tag, final boolean allowStateLoss) {
    final OHCompactAlertDialogFragment fragment;
    if (mObjectP == null) {
        fragment = OHCompactAlertDialogFragment.newInstance(tag, mTitle, mMessage, mButton1Text, mButton2Text,
                mSelectableItems, mSelectableItemsRes, mSelectableItemIconsRes, mObjectS, mCancelable);
    } else {
        fragment = OHCompactAlertDialogFragment.newInstance(tag, mTitle, mMessage, mButton1Text, mButton2Text,
                mSelectableItems, mSelectableItemsRes, mSelectableItemIconsRes, mObjectP, mCancelable);
    }
    final FragmentTransaction ft = manager.beginTransaction();
    ft.add(fragment, tag);
    if (allowStateLoss) {
        ft.commitAllowingStateLoss();
    } else {
        ft.commit();
    }
    return fragment;
}
 
Example 7
Source File: LaunchActivity.java    From Yahala-Messenger with MIT License 5 votes vote down vote up
public void presentFragment(BaseFragment fragment, String tag, boolean removeLast, boolean bySwipe) {
    if (getCurrentFocus() != null) {
        OSUtilities.hideKeyboard(getCurrentFocus());
    }
    if (!fragment.onFragmentCreate()) {
        return;
    }
    //fragment.setParentActivity(this);
    BaseFragment current = null;
    if (!ApplicationLoader.fragmentsStack.isEmpty()) {
        current = ApplicationLoader.fragmentsStack.get(ApplicationLoader.fragmentsStack.size() - 1);
    }
    if (current != null) {
        current.willBeHidden();
    }
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction fTrans = fm.beginTransaction();
    if (removeLast && current != null) {
        ApplicationLoader.fragmentsStack.remove(ApplicationLoader.fragmentsStack.size() - 1);
        current.onFragmentDestroy();
    }
    SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
    boolean animations = preferences.getBoolean("view_animations", true);
    if (animations) {
        if (bySwipe) {

            fTrans.setCustomAnimations(R.anim.no_anim, R.anim.no_anim);
        } else {
            fTrans.setCustomAnimations(R.anim.scale_in_out_d, R.anim.scale_out_in_d);
            //fTrans.setCustomAnimations(R.anim.scale_in ,R.anim.scale_out);
        }
    }
    try {
        fTrans.replace(R.id.container, fragment, tag);
        fTrans.commitAllowingStateLoss();
    } catch (Exception e) {
        FileLog.e("yahala", e);
    }
    ApplicationLoader.fragmentsStack.add(fragment);
}
 
Example 8
Source File: BaseDialogFragment.java    From YCDialog with Apache License 2.0 5 votes vote down vote up
public void show(FragmentManager fragmentManager) {
    DialogUtils.checkMainThread();
    if(fragmentManager!=null){
        //show(fragmentManager, getFragmentTag());

        //主要是为了解决Can not perform this action after onSaveInstanceState异常
        //发生场景:Activity即将被销毁,再给它添加Fragment就会出错。
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.add(this, getFragmentTag());
        ft.commitAllowingStateLoss();
    }else {
        throw new NullPointerException("需要设置setFragmentManager");
        //ToastUtils.showToast("需要设置setFragmentManager");
    }
}
 
Example 9
Source File: FragmentTestActivity.java    From Android-PickerView with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragmenttest);
    mFragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.frame_activity_main, new TestFragment());
    fragmentTransaction.commitAllowingStateLoss();

}
 
Example 10
Source File: DetailVideoActivity.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * 展示页面
 */
private void showPlayingFragment() {
    if (isPlayFragmentShow) {
        return;
    }

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.setCustomAnimations(R.anim.fragment_slide_up, 0);
    if (mDetailAudioFragment == null) {
        mDetailAudioFragment = DetailAudioFragment.newInstance("Video");
        ft.add(android.R.id.content, mDetailAudioFragment);
    } else {
        ft.show(mDetailAudioFragment);
    }
    ft.commitAllowingStateLoss();
    isPlayFragmentShow = true;
    AppLogUtils.e("fragment数量+DetailVideoActivity" + FragmentUtils.getAllFragments(getSupportFragmentManager()).size());

    if (videoPlayer.isPlaying() || videoPlayer.isBufferingPlaying()) {
        videoPlayer.pause();
    }

    //当视频正在播放,准备播放时,点击音视频切换按钮,先暂停视频,然后记录视频播放位置,show音频播放页面
    //当视频已经暂停,播放错误,播放停止时,点击音视频切换按钮,直接记录视频播放位置,show音频播放页面
    BaseConfig.INSTANCE.setPosition(videoPlayer.getCurrentPosition());
    AppLogUtils.e("播放位置----视频页开始显示音频--" + videoPlayer.getCurrentPosition());

    if (mDetailAudioFragment != null) {
        mDetailAudioFragment.setViewData(BaseAppHelper.get().getMusicList().get(0));
        if (getPlayService().isDefault() || getPlayService().isPausing()) {
            getPlayService().seekTo((int) BaseConfig.INSTANCE.getPosition());
            getPlayService().playPause();
        }
    }
}
 
Example 11
Source File: MainActivity.java    From YCAudioPlayer with Apache License 2.0 5 votes vote down vote up
/**
 * 隐藏页面
 */
private void hidePlayingFragment() {
    if(mPlayFragment!=null){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(0, R.anim.fragment_slide_down);
        ft.hide(mPlayFragment);
        ft.commitAllowingStateLoss();
        isPlayFragmentShow = false;
    }
}
 
Example 12
Source File: RMBTMainActivity.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
    switch (requestCode)
    {
    case PermissionHelper.REQUEST_AT_INIT:
        if (geoLocation == null)
            geoLocation = new MainGeoLocation(getApplicationContext());
        geoLocation.start();
        checkSettings(true, null);
        
        
        // reinit main menu fragment mainly to update location info
        final FragmentTransaction ft;
        ft = fm.beginTransaction();
        ft.replace(R.id.fragment_content, new RMBTMainMenuFragment(), AppConstants.PAGE_TITLE_MAIN);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        //hack for v4 support library bug:
        ft.commitAllowingStateLoss();
        //ft.commit();
        
        return;
    
    case PermissionHelper.REQUEST_AT_TEST_START:
        startTest();
        return;
    }
}
 
Example 13
Source File: BaseActivity.java    From timecat with Apache License 2.0 5 votes vote down vote up
public void switchFragment(Fragment fragment) {
    if (currentFragment != null && currentFragment == fragment) {
        return;
    }
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    if (currentFragment != null) {
        ft.hide(currentFragment);
    }
    ft.show(fragment);
    ft.commitAllowingStateLoss();
    currentFragment = fragment;
}
 
Example 14
Source File: FragmentTestActivity.java    From Android-PickerView with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragmenttest);
    mFragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.frame_activity_main, new TestFragment());
    fragmentTransaction.commitAllowingStateLoss();

}
 
Example 15
Source File: ExampleContainerActivity.java    From ShadowDrawable with MIT License 5 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_example_container_layout);
	int containerType = getIntent().getIntExtra("containerType", CONTAINER_TYPE_LIST);
	FragmentManager fm = getSupportFragmentManager();
	FragmentTransaction ft = fm.beginTransaction();
	Fragment fragment = null;
	if (containerType == CONTAINER_TYPE_LIST) {
		fragment = new ListExampleFragment();
	} else if (containerType == CONTAINER_TYPE_GRID) {
		fragment = new GridExampleFragment();
	} else if (containerType == CONTAINER_TYPE_RECYCLE) {
		fragment = new RecycleExampleFragment();
	} else if (containerType == CONTAINER_TYPE_PAGER) {
		fragment = new PagerExampleFragment();
	} else {
		/**
		 * Nothing
		 */
	}

	if (fragment != null) {
		ft.add(R.id.container_layout, fragment);
		ft.commitAllowingStateLoss();
	}
}
 
Example 16
Source File: BitBaseDialogFragment.java    From tysq-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 显示对话框
 */
public BitBaseDialogFragment showWithAllow(Fragment fragment) {
    FragmentTransaction ft = fragment.getChildFragmentManager().beginTransaction();
    ft.add(this, TAG);
    ft.commitAllowingStateLoss();
    return this;
}
 
Example 17
Source File: WallpaperBoardActivity.java    From wallpaperboard with Apache License 2.0 5 votes vote down vote up
private void setFragment(Fragment fragment) {
    if (fragment == null) return;
    clearBackStack();

    FragmentTransaction ft = mFragManager.beginTransaction().replace(
            R.id.container, fragment, mFragmentTag);
    try {
        ft.commit();
    } catch (Exception e) {
        ft.commitAllowingStateLoss();
    }

    mNavigationView.getMenu().getItem(mPosition).setChecked(true);
}
 
Example 18
Source File: Fragment2.java    From chameleon with Apache License 2.0 5 votes vote down vote up
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.fragment_container, new Fragment3());
    fragmentTransaction.commitAllowingStateLoss();

}
 
Example 19
Source File: FragmentUtility.java    From scene with Apache License 2.0 5 votes vote down vote up
static void commitFragment(@NonNull FragmentTransaction transaction, boolean commitNow) {
    if (commitNow) {
        transaction.commitNowAllowingStateLoss();
    } else {
        transaction.commitAllowingStateLoss();
    }
}
 
Example 20
Source File: FragmentUtil.java    From DeviceInfo with Apache License 2.0 4 votes vote down vote up
private void pushFragment(final Fragment fragment, final Fragment parentFragment, final int containerId, boolean isAddToBackStack, boolean isJustAdd, final boolean shouldAnimate, final boolean ignorIfCurrent)
    {
        if (fragment == null)
            return;
        // Add the fragment to the 'fragment_container' FrameLayout
        final FragmentManager fragmentManager;// = getSupportFragmentManager();

        if (parentFragment != null)
        {
            fragmentManager = parentFragment.getChildFragmentManager();
        }
        else {
            fragmentManager = mActivity.getSupportFragmentManager();
        }


        // Find current visible fragment
        Fragment fragmentCurrent = fragmentManager.findFragmentById(R.id.fragment_container);

        if (ignorIfCurrent && fragmentCurrent!=null) {
            if (fragment.getClass().getCanonicalName().equalsIgnoreCase(fragmentCurrent.getTag())) {
                return;
            }
        }


        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        if (shouldAnimate) {
//            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out);
        } else {
            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        }

        if (fragmentCurrent != null)
        {
            fragmentTransaction.hide(fragmentCurrent);
        }

        if (isAddToBackStack) {
            fragmentTransaction.addToBackStack(fragment.getClass().getCanonicalName());
        }
        else {

           /* List<Fragment> fragmentList = fragmentManager.getFragments();
            if (fragmentList != null && !fragmentList.isEmpty())
            {
//                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                for (Fragment fragmentM : fragmentList)
                {
                    if (fragmentM != null)
                    {
                        fragmentTransaction.remove(fragmentM);
                    }
                }
//                fragmentTransaction.commit();
            }*/
        }

        if (isJustAdd) {
            fragmentTransaction.add(containerId, fragment, fragment.getClass().getCanonicalName());
        }
        else {
            fragmentTransaction.replace(containerId, fragment, fragment.getClass().getCanonicalName());
        }


        try {
//            fragmentTransaction.commit();
            fragmentTransaction.commitAllowingStateLoss();

            if (!isAddToBackStack) {
//                fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

            }

            Methods.hideKeyboard(mActivity);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }