UPDATE: Extract the ViewModelStoreOwner role from LifecycleActivity & LifecycleFragment into derived LifecycleViewModelActivity & LifecycleViewModelFragment.

This commit is contained in:
Oasis 2019-07-09 11:18:29 +08:00
parent ba54ff06b3
commit 1c58ab64dd
5 changed files with 60 additions and 38 deletions

View File

@ -8,28 +8,13 @@ import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;
/**
* Extends the native {@link Activity} with compatibility for lifecycle and view-model in Android Jet Pack.
* Extends the native {@link Activity} with compatibility for lifecycle in Android Jet Pack.
*
* Created by Oasis on 2018/5/17.
*/
public class LifecycleActivity extends Activity implements LifecycleOwner, ViewModelStoreOwner {
@Override public @NonNull ViewModelStore getViewModelStore() {
if (getApplication() == null) {
throw new IllegalStateException("Your activity is not yet attached to the "
+ "Application instance. You can't request ViewModel before onCreate call.");
}
if (mViewModelStore == null) {
mViewModelStore = new ViewModelStore();
}
return mViewModelStore;
}
private ViewModelStore mViewModelStore;
public class LifecycleActivity extends Activity implements LifecycleOwner {
/* Lifecycle */

View File

@ -10,27 +10,14 @@ import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.M;
/**
* Extends the native {@link Fragment} with compatibility for lifecycle and view-model in Android Jet Pack.
* Extends the native {@link Fragment} with compatibility for lifecycle in Android Jet Pack.
*
* Created by Oasis on 2018/5/17.
*/
@ParametersAreNonnullByDefault
public class LifecycleFragment extends Fragment implements LifecycleOwner, ViewModelStoreOwner {
@Override public @NonNull ViewModelStore getViewModelStore() {
if ((SDK_INT >= M ? getContext() : getActivity()) == null) throw new IllegalStateException("Can't access ViewModels from detached fragment");
if (mViewModelStore == null) mViewModelStore = new ViewModelStore();
return mViewModelStore;
}
private ViewModelStore mViewModelStore; // TODO: Retain across configuration changes
public class LifecycleFragment extends Fragment implements LifecycleOwner {
@Override public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);

View File

@ -0,0 +1,26 @@
package com.oasisfeng.android.app;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;
/**
* Extends the {@link LifecycleActivity} with compatibility for view-model in Android Jet Pack.
*
* Created by Oasis on 2019-7-9.
*/
public class LifecycleViewModelActivity extends LifecycleActivity implements ViewModelStoreOwner {
@Override public @NonNull ViewModelStore getViewModelStore() {
if (getApplication() == null) {
throw new IllegalStateException("Your activity is not yet attached to the "
+ "Application instance. You can't request ViewModel before onCreate call.");
}
if (mViewModelStore == null) {
mViewModelStore = new ViewModelStore();
}
return mViewModelStore;
}
private ViewModelStore mViewModelStore;
}

View File

@ -0,0 +1,24 @@
package com.oasisfeng.android.app;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.M;
/**
* Extends the {@link LifecycleFragment} with compatibility for view-model in Android Jet Pack.
*
* Created by Oasis on 2019-7-9.
*/
public class LifecycleViewModelFragment extends LifecycleFragment implements ViewModelStoreOwner {
@Override public @NonNull ViewModelStore getViewModelStore() {
if ((SDK_INT >= M ? getContext() : getActivity()) == null) throw new IllegalStateException("Can't access ViewModels from detached fragment");
if (mViewModelStore == null) mViewModelStore = new ViewModelStore();
return mViewModelStore;
}
private ViewModelStore mViewModelStore; // TODO: Retain across configuration changes
}

View File

@ -20,8 +20,8 @@ import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import com.oasisfeng.android.app.LifecycleActivity;
import com.oasisfeng.android.app.LifecycleFragment;
import com.oasisfeng.android.app.LifecycleViewModelActivity;
import com.oasisfeng.android.app.LifecycleViewModelFragment;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
@ -65,7 +65,7 @@ public class ViewModelProviders {
*/
@NonNull
@MainThread
public static ViewModelProvider of(@NonNull LifecycleFragment fragment) {
public static ViewModelProvider of(@NonNull LifecycleViewModelFragment fragment) {
return of(fragment, null);
}
@ -80,7 +80,7 @@ public class ViewModelProviders {
*/
@NonNull
@MainThread
public static ViewModelProvider of(@NonNull LifecycleActivity activity) {
public static ViewModelProvider of(@NonNull LifecycleViewModelActivity activity) {
return of(activity, null);
}
@ -96,7 +96,7 @@ public class ViewModelProviders {
*/
@NonNull
@MainThread
public static ViewModelProvider of(@NonNull LifecycleFragment fragment, @Nullable ViewModelProvider.Factory factory) {
public static ViewModelProvider of(@NonNull LifecycleViewModelFragment fragment, @Nullable ViewModelProvider.Factory factory) {
Application application = checkApplication(checkActivity(fragment));
if (factory == null) {
factory = ViewModelProvider.AndroidViewModelFactory.getInstance(application);
@ -116,7 +116,7 @@ public class ViewModelProviders {
*/
@NonNull
@MainThread
public static ViewModelProvider of(@NonNull LifecycleActivity activity,
public static ViewModelProvider of(@NonNull LifecycleViewModelActivity activity,
@Nullable ViewModelProvider.Factory factory) {
Application application = checkApplication(activity);
if (factory == null) {