This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HolderFragment holderFragmentFor(Fragment parentFragment) { | |
FragmentManager fm = parentFragment.getChildFragmentManager(); | |
HolderFragment holder = findHolderFragment(fm); | |
if (holder != null) { | |
return holder; | |
} | |
holder = mNotCommittedFragmentHolders.get(parentFragment); | |
if (holder != null) { | |
return holder; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public HolderFragment() { | |
setRetainInstance(true); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void onDestroy() { | |
mCalled = true; | |
// Use mStateSaved instead of isStateSaved() since we're past onStop() | |
if (mViewModelStore != null && !mHost.mFragmentManager.mStateSaved) { | |
mViewModelStore.clear(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NameViewModel extends ViewModel { | |
private MutableLiveData<String> mCurrentName; | |
public MutableLiveData<String> getCurrentName() { | |
if (mCurrentName == null) { | |
mCurrentName = new MutableLiveData<String>(); | |
} | |
return mCurrentName; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@MainThread | |
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<T> observer) { | |
if (owner.getLifecycle().getCurrentState() == DESTROYED) { | |
// ignore | |
return; | |
} | |
LifecycleBoundObserver wrapper = new LifecycleBoundObserver(owner, observer); | |
ObserverWrapper existing = mObservers.putIfAbsent(observer, wrapper); | |
if (existing != null && !existing.isAttachedTo(owner)) { | |
throw new IllegalArgumentException("Cannot add the same observer" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void setValue(T value) { | |
assertMainThread("setValue"); | |
mVersion++; | |
mData = value; | |
dispatchingValue(null); | |
} | |
private void dispatchingValue(@Nullable ObserverWrapper initiator) { | |
if (mDispatchingValue) { | |
mDispatchInvalidated = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LifecycleBoundObserver extends ObserverWrapper implements GenericLifecycleObserver { | |
@NonNull final LifecycleOwner mOwner; | |
LifecycleBoundObserver(@NonNull LifecycleOwner owner, Observer<T> observer) { | |
super(observer); | |
mOwner = owner; | |
} | |
@Override | |
boolean shouldBeActive() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SupportActivity extends Activity implements LifecycleOwner { | |
... | |
private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this); | |
... | |
@Override | |
protected void onSaveInstanceState(Bundle outState) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestrictTo(LIBRARY_GROUP) | |
public class SupportActivity extends Activity implements LifecycleOwner { | |
// ... | |
@Override | |
@SuppressWarnings("RestrictedApi") | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ReportFragment.injectIfNeededIn(this); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void activeStateChanged(boolean newActive) { | |
if (newActive == mActive) { | |
return; | |
} | |
// immediately set active state, so we'd never dispatch anything to inactive | |
// owner | |
mActive = newActive; | |
boolean wasInactive = LiveData.this.mActiveCount == 0; | |
LiveData.this.mActiveCount += mActive ? 1 : -1; | |
if (wasInactive && mActive) { |