|
| 1 | +package com.blankj.utilcode.utils; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.app.Fragment; |
| 5 | +import android.app.FragmentManager; |
| 6 | +import android.app.FragmentTransaction; |
| 7 | +import android.os.Bundle; |
| 8 | + |
| 9 | +/** |
| 10 | + * <pre> |
| 11 | + * author: Blankj |
| 12 | + * blog : http://blankj.com |
| 13 | + * time : 2017/1/17 |
| 14 | + * desc : |
| 15 | + * </pre> |
| 16 | + */ |
| 17 | +public class FragmentUtils { |
| 18 | + |
| 19 | + private FragmentUtils() { |
| 20 | + throw new UnsupportedOperationException("u can't instantiate me..."); |
| 21 | + } |
| 22 | + |
| 23 | + public static Fragment replaceFragment(FragmentManager fragmentManager, |
| 24 | + int container, |
| 25 | + Fragment newFragment) { |
| 26 | + return replaceFragment(fragmentManager, container, newFragment, false); |
| 27 | + } |
| 28 | + |
| 29 | + public static Fragment replaceFragment(FragmentManager fragmentManager, |
| 30 | + int container, |
| 31 | + Fragment newFragment, |
| 32 | + boolean addToBackStack) { |
| 33 | + final FragmentTransaction transaction = fragmentManager.beginTransaction(); |
| 34 | + final String tag = newFragment.getClass().getSimpleName(); |
| 35 | + if (newFragment != null) { |
| 36 | + transaction.replace(container, newFragment, tag); |
| 37 | + } |
| 38 | + if (addToBackStack) { |
| 39 | + transaction.addToBackStack(null); |
| 40 | + } |
| 41 | + transaction.commitAllowingStateLoss(); |
| 42 | + return newFragment; |
| 43 | + } |
| 44 | + |
| 45 | + public static Fragment replaceFragment(FragmentManager fragmentManager, |
| 46 | + int container, |
| 47 | + Class<? extends Fragment> newFragment, |
| 48 | + Bundle args) { |
| 49 | + return replaceFragment(fragmentManager, container, newFragment, args, false); |
| 50 | + } |
| 51 | + |
| 52 | + public static Fragment replaceFragment(FragmentManager fragmentManager, |
| 53 | + int container, Class<? extends Fragment> newFragment, |
| 54 | + Bundle args, boolean addToBackStack) { |
| 55 | + Fragment fragment = null; |
| 56 | + // 构造新的Fragment |
| 57 | + try { |
| 58 | + fragment = newFragment.newInstance(); |
| 59 | + } catch (InstantiationException e) { |
| 60 | + e.printStackTrace(); |
| 61 | + } catch (IllegalAccessException e) { |
| 62 | + e.printStackTrace(); |
| 63 | + } |
| 64 | + if (fragment != null) { |
| 65 | + // 设置参数 |
| 66 | + if (args != null && !args.isEmpty()) { |
| 67 | + final Bundle bundle = fragment.getArguments(); |
| 68 | + if (bundle != null) { |
| 69 | + bundle.putAll(args); |
| 70 | + } else { |
| 71 | + fragment.setArguments(args); |
| 72 | + } |
| 73 | + } |
| 74 | + // 替换 |
| 75 | + return replaceFragment(fragmentManager, container, fragment, addToBackStack); |
| 76 | + } else { |
| 77 | + return null; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + public static Fragment switchFragment(FragmentManager fragmentManager, |
| 83 | + int container, |
| 84 | + Fragment currentFragment, |
| 85 | + Class<? extends Fragment> newFragment, |
| 86 | + Bundle args) { |
| 87 | + return switchFragment(fragmentManager, container, currentFragment, newFragment, args, false); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param fragmentManager |
| 92 | + * @param container |
| 93 | + * @param currentFragment |
| 94 | + * @param newFragment |
| 95 | + * @param args 新Fragment的参数 |
| 96 | + * @param addToBackStack 这个操作是否加入栈中,如果要实现类似返回效果,则需要�? |
| 97 | + * @return 新显示的Fragment |
| 98 | + */ |
| 99 | + public static Fragment switchFragment(FragmentManager fragmentManager, |
| 100 | + int container, |
| 101 | + Fragment currentFragment, |
| 102 | + Class<? extends Fragment> newFragment, |
| 103 | + Bundle args, |
| 104 | + boolean addToBackStack) { |
| 105 | + final FragmentTransaction transaction = fragmentManager.beginTransaction(); |
| 106 | + final String tag = newFragment.getSimpleName(); |
| 107 | + Fragment fragment = fragmentManager.findFragmentByTag(tag); |
| 108 | + // 如果在栈中找到相应的Fragment,则显示,否则重新生成一�? |
| 109 | + if (fragment != null) { |
| 110 | + if (fragment != currentFragment) { |
| 111 | + if (currentFragment != null) { |
| 112 | + transaction.hide(currentFragment); |
| 113 | + } |
| 114 | + transaction.show(fragment); |
| 115 | + if (addToBackStack) { |
| 116 | + transaction.addToBackStack(null); |
| 117 | + } |
| 118 | + transaction.commitAllowingStateLoss(); |
| 119 | + } else { |
| 120 | + fragment.getArguments().putAll(args); |
| 121 | + } |
| 122 | + |
| 123 | + return fragment; |
| 124 | + } else { |
| 125 | + try { |
| 126 | + fragment = newFragment.newInstance(); |
| 127 | + } catch (InstantiationException e) { |
| 128 | + e.printStackTrace(); |
| 129 | + } catch (IllegalAccessException e) { |
| 130 | + e.printStackTrace(); |
| 131 | + } |
| 132 | + } |
| 133 | + // 为新的Fragment添加参数 |
| 134 | + if (fragment != null) { |
| 135 | + if (args != null && !args.isEmpty()) { |
| 136 | + final Bundle bundle = fragment.getArguments(); |
| 137 | + if (bundle != null) { |
| 138 | + bundle.putAll(args); |
| 139 | + } else { |
| 140 | + fragment.setArguments(args); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + // 显示新的Fragment |
| 145 | + if (currentFragment != null) { |
| 146 | + transaction.hide(currentFragment); |
| 147 | + } |
| 148 | + transaction.add(container, fragment, tag); |
| 149 | + if (addToBackStack) { |
| 150 | + transaction.addToBackStack(null); |
| 151 | + } |
| 152 | + transaction.commitAllowingStateLoss(); |
| 153 | + return fragment; |
| 154 | + } |
| 155 | +} |
0 commit comments