[关闭]
@RitcheeQinG 2020-09-07T02:12:33.000000Z 字数 2664 阅读 396

Fragment初始化传参的正确姿势

Android


FragmentActivity

  1. @Override
  2. protected void onCreate(@Nullable Bundle savedInstanceState) {
  3. mFragments.attachHost(null /*parent*/);
  4. if (savedInstanceState != null) {
  5. Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
  6. mFragments.restoreSaveState(p);
  7. // ...
  8. }
  9. }

这里的 mFragmentsFragmentController

  1. /**
  2. * Restores the saved state for all Fragments.
  3. *
  4. * @param state the saved state containing the Parcelable returned by {@link #saveAllState()}
  5. * @see #saveAllState()
  6. */
  7. public void restoreSaveState(@Nullable Parcelable state) {
  8. if (!(mHost instanceof ViewModelStoreOwner)) {
  9. throw new IllegalStateException("Your FragmentHostCallback must implement "
  10. + "ViewModelStoreOwner to call restoreSaveState(). Call restoreAllState() "
  11. + " if you're still using retainNestedNonConfig().");
  12. }
  13. mHost.mFragmentManager.restoreSaveState(state);
  14. }

FragmentManagerImpl

  1. void restoreSaveState(Parcelable state) {
  2. // ... 主要看这里
  3. // Build the full list of active fragments, instantiating them from
  4. // their saved state.
  5. mActive.clear();
  6. for (FragmentState fs : fms.mActive) {
  7. if (fs != null) {
  8. Fragment f = fs.instantiate(mHost.getContext().getClassLoader(),
  9. getFragmentFactory());
  10. f.mFragmentManager = this;
  11. if (DEBUG) Log.v(TAG, "restoreSaveState: active (" + f.mWho + "): " + f);
  12. mActive.put(f.mWho, f);
  13. // Now that the fragment is instantiated (or came from being
  14. // retained above), clear mInstance in case we end up re-restoring
  15. // from this FragmentState again.
  16. fs.mInstance = null;
  17. }
  18. }
  19. // ...
  20. }

FragmentState

  1. public Fragment instantiate(@NonNull ClassLoader classLoader,
  2. @NonNull FragmentFactory factory) {
  3. if (mInstance == null) {
  4. if (mArguments != null) {
  5. mArguments.setClassLoader(classLoader);
  6. }
  7. mInstance = factory.instantiate(classLoader, mClassName);
  8. mInstance.setArguments(mArguments);
  9. if (mSavedFragmentState != null) {
  10. mSavedFragmentState.setClassLoader(classLoader);
  11. mInstance.mSavedFragmentState = mSavedFragmentState;
  12. } else {
  13. // When restoring a Fragment, always ensure we have a
  14. // non-null Bundle so that developers have a signal for
  15. // when the Fragment is being restored
  16. mInstance.mSavedFragmentState = new Bundle();
  17. }
  18. // ...
  19. // 到这里应该挺明白了
  20. // Fragment没有默认构造就会报错,构造传参最好是走Argument
  21. }
  22. return mInstance;
  23. }

FragmentFactory

  1. /**
  2. * Create a new instance of a Fragment with the given class name. This uses
  3. * {@link #loadFragmentClass(ClassLoader, String)} and the empty
  4. * constructor of the resulting Class by default.
  5. *
  6. * @param classLoader The default classloader to use for instantiation
  7. * @param className The class name of the fragment to instantiate.
  8. * @return Returns a new fragment instance.
  9. * @throws Fragment.InstantiationException If there is a failure in instantiating
  10. * the given fragment class. This is a runtime exception; it is not
  11. * normally expected to happen.
  12. */
  13. @NonNull
  14. public Fragment instantiate(@NonNull ClassLoader classLoader, @NonNull String className) {
  15. try {
  16. Class<? extends Fragment> cls = loadFragmentClass(classLoader, className);
  17. return cls.getConstructor().newInstance();
  18. }
  19. // ...
  20. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注