LiveData和ViewModel源码学习

这篇具有很好参考价值的文章主要介绍了LiveData和ViewModel源码学习。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

LiveData
ObserverWrapper 数据观察者
LifecycleEventObserver 生命周期观察者
  1. 活跃状态接受并更新数据 刷新页面数据
  2. 非活跃状态暂停接收数据 防止崩溃,
  3. 销毁状态的时候移除观察者 防止内存泄漏
  4. 数据可以监听
  5. 数据倒灌,就是先发送数据,通过生命周期响应来触发change
  6. 数据粘性,先发送数据,后订阅的也能收到数据
  7. 为什么能感知生命周期和数据观察者 因为把传入的观察者通过LifecicleBoundObserver包了一层
    LifecicleBoundObserver继承了LifecicleEventObserver和ObserverWrapper,然后添加到lifecicle和livedata中


package androidx.lifecycle;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.arch.core.internal.SafeIterableMap;
import androidx.lifecycle.Lifecycle.State;
import java.util.Iterator;
import java.util.Map;

public abstract class LiveData<T> {
    final Object mDataLock = new Object();
    static final int START_VERSION = -1;
    static final Object NOT_SET = new Object();
    private SafeIterableMap<Observer<? super T>, LiveData<T>.ObserverWrapper> mObservers = new SafeIterableMap();
    int mActiveCount = 0;
    private volatile Object mData;
    volatile Object mPendingData;
    private int mVersion;
    private boolean mDispatchingValue;
    private boolean mDispatchInvalidated;
    private final Runnable mPostValueRunnable;

    public LiveData(T value) {
        this.mPendingData = NOT_SET;
        this.mPostValueRunnable = new NamelessClass_1();
        this.mData = value;
        this.mVersion = 0;
    }

    public LiveData() {
        this.mPendingData = NOT_SET;

        class NamelessClass_1 implements Runnable {
            NamelessClass_1() {
            }

            public void run() {
                Object newValue;
                synchronized(LiveData.this.mDataLock) {
                    newValue = LiveData.this.mPendingData;
                    LiveData.this.mPendingData = LiveData.NOT_SET;
                }

                LiveData.this.setValue(newValue);
            }
        }

        this.mPostValueRunnable = new NamelessClass_1();
        this.mData = NOT_SET;
        this.mVersion = -1;
    }

    private void considerNotify(LiveData<T>.ObserverWrapper observer) {
        if (observer.mActive) {
            if (!observer.shouldBeActive()) {
                observer.activeStateChanged(false);
            } else if (observer.mLastVersion < this.mVersion) {
                observer.mLastVersion = this.mVersion;
                observer.mObserver.onChanged(this.mData);
            }
        }
    }

    void dispatchingValue(@Nullable LiveData<T>.ObserverWrapper initiator) {
        if (this.mDispatchingValue) {
            this.mDispatchInvalidated = true;
        } else {
            this.mDispatchingValue = true;

            do {
                this.mDispatchInvalidated = false;
                if (initiator != null) {
                    this.considerNotify(initiator);
                    initiator = null;
                } else {
                    Iterator<Map.Entry<Observer<? super T>, LiveData<T>.ObserverWrapper>> iterator = this.mObservers.iteratorWithAdditions();

                    while(iterator.hasNext()) {
                        this.considerNotify((ObserverWrapper)((Map.Entry)iterator.next()).getValue());
                        if (this.mDispatchInvalidated) {
                            break;
                        }
                    }
                }
            } while(this.mDispatchInvalidated);

            this.mDispatchingValue = false;
        }
    }

    @MainThread
    public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
        assertMainThread("observe");
        if (owner.getLifecycle().getCurrentState() != State.DESTROYED) {
            LiveData<T>.LifecycleBoundObserver wrapper = new LifecycleBoundObserver(owner, observer);
            LiveData<T>.ObserverWrapper existing = (ObserverWrapper)this.mObservers.putIfAbsent(observer, wrapper);
            if (existing != null && !existing.isAttachedTo(owner)) {
                throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
            } else if (existing == null) {
                owner.getLifecycle().addObserver(wrapper);
            }
        }
    }

    @MainThread
    public void observeForever(@NonNull Observer<? super T> observer) {
        assertMainThread("observeForever");
        LiveData<T>.AlwaysActiveObserver wrapper = new AlwaysActiveObserver(observer);
        LiveData<T>.ObserverWrapper existing = (ObserverWrapper)this.mObservers.putIfAbsent(observer, wrapper);
        if (existing instanceof LifecycleBoundObserver) {
            throw new IllegalArgumentException("Cannot add the same observer with different lifecycles");
        } else if (existing == null) {
            wrapper.activeStateChanged(true);
        }
    }

    @MainThread
    public void removeObserver(@NonNull Observer<? super T> observer) {
        assertMainThread("removeObserver");
        LiveData<T>.ObserverWrapper removed = (ObserverWrapper)this.mObservers.remove(observer);
        if (removed != null) {
            removed.detachObserver();
            removed.activeStateChanged(false);
        }
    }

    @MainThread
    public void removeObservers(@NonNull LifecycleOwner owner) {
        assertMainThread("removeObservers");
        Iterator var2 = this.mObservers.iterator();

        while(var2.hasNext()) {
            Map.Entry<Observer<? super T>, LiveData<T>.ObserverWrapper> entry = (Map.Entry)var2.next();
            if (((ObserverWrapper)entry.getValue()).isAttachedTo(owner)) {
                this.removeObserver((Observer)entry.getKey());
            }
        }

    }

    protected void postValue(T value) {
        boolean postTask;
        synchronized(this.mDataLock) {
            postTask = this.mPendingData == NOT_SET;
            this.mPendingData = value;
        }

        if (postTask) {
            ArchTaskExecutor.getInstance().postToMainThread(this.mPostValueRunnable);
        }
    }

    @MainThread
    protected void setValue(T value) {
        assertMainThread("setValue");
        ++this.mVersion;
        this.mData = value;
        this.dispatchingValue((ObserverWrapper)null);
    }

    @Nullable
    public T getValue() {
        Object data = this.mData;
        return data != NOT_SET ? data : null;
    }

    int getVersion() {
        return this.mVersion;
    }

    protected void onActive() {
    }

    protected void onInactive() {
    }

    public boolean hasObservers() {
        return this.mObservers.size() > 0;
    }

    public boolean hasActiveObservers() {
        return this.mActiveCount > 0;
    }

    static void assertMainThread(String methodName) {
        if (!ArchTaskExecutor.getInstance().isMainThread()) {
            throw new IllegalStateException("Cannot invoke " + methodName + " on a background thread");
        }
    }

    private class AlwaysActiveObserver extends LiveData<T>.ObserverWrapper {
        AlwaysActiveObserver(Observer<? super T> observer) {
            super(observer);
        }

        boolean shouldBeActive() {
            return true;
        }
    }

    private abstract class ObserverWrapper {
        final Observer<? super T> mObserver;
        boolean mActive;
        int mLastVersion = -1;

        ObserverWrapper(Observer<? super T> observer) {
            this.mObserver = observer;
        }

        abstract boolean shouldBeActive();

        boolean isAttachedTo(LifecycleOwner owner) {
            return false;
        }

        void detachObserver() {
        }

        void activeStateChanged(boolean newActive) {
            if (newActive != this.mActive) {
                this.mActive = newActive;
                boolean wasInactive = LiveData.this.mActiveCount == 0;
                LiveData var10000 = LiveData.this;
                var10000.mActiveCount += this.mActive ? 1 : -1;
                if (wasInactive && this.mActive) {
                    LiveData.this.onActive();
                }

                if (LiveData.this.mActiveCount == 0 && !this.mActive) {
                    LiveData.this.onInactive();
                }

                if (this.mActive) {
                    LiveData.this.dispatchingValue(this);
                }

            }
        }
    }

    class LifecycleBoundObserver extends LiveData<T>.ObserverWrapper implements LifecycleEventObserver {
        @NonNull
        final LifecycleOwner mOwner;

        LifecycleBoundObserver(@NonNull LifecycleOwner owner, Observer<? super T> observer) {
            super(observer);
            this.mOwner = owner;
        }

        boolean shouldBeActive() {
            return this.mOwner.getLifecycle().getCurrentState().isAtLeast(State.STARTED);
        }

        public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
            if (this.mOwner.getLifecycle().getCurrentState() == State.DESTROYED) {
                LiveData.this.removeObserver(this.mObserver);
            } else {
                this.activeStateChanged(this.shouldBeActive());
            }
        }

        boolean isAttachedTo(LifecycleOwner owner) {
            return this.mOwner == owner;
        }

        void detachObserver() {
            this.mOwner.getLifecycle().removeObserver(this);
        }
    }
}

ViewModel

1.为什么ViewModel生命周期比activity长
attach(Acitivity)->mLastNonConfigurationInstances(ActivityClientRecord)-> 恢复
performDestoryActivity(ActivityThread)-> retainNonConfigurationInstances(Activity)- onRetainConfigurationInstances(ComponentActivity)->mLastNonConfigurationInstances 保存文章来源地址https://www.toymoban.com/news/detail-848594.html

ViewModelStore
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package androidx.lifecycle;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class ViewModelStore {
    private final HashMap<String, ViewModel> mMap = new HashMap();

    public ViewModelStore() {
    }

    final void put(String key, ViewModel viewModel) {
        ViewModel oldViewModel = (ViewModel)this.mMap.put(key, viewModel);
        if (oldViewModel != null) {
            oldViewModel.onCleared();
        }

    }

    final ViewModel get(String key) {
        return (ViewModel)this.mMap.get(key);
    }

    Set<String> keys() {
        return new HashSet(this.mMap.keySet());
    }

    public final void clear() {
        Iterator var1 = this.mMap.values().iterator();

        while(var1.hasNext()) {
            ViewModel vm = (ViewModel)var1.next();
            vm.clear();
        }

        this.mMap.clear();
    }
}

ViewModelProvider


package androidx.lifecycle;

import android.app.Application;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import java.lang.reflect.InvocationTargetException;

public class ViewModelProvider {
    private static final String DEFAULT_KEY = "androidx.lifecycle.ViewModelProvider.DefaultKey";
    private final Factory mFactory;
    private final ViewModelStore mViewModelStore;

    public ViewModelProvider(@NonNull ViewModelStoreOwner owner) {
        this((ViewModelStore)owner.getViewModelStore(), (Factory)(owner instanceof HasDefaultViewModelProviderFactory ? ((HasDefaultViewModelProviderFactory)owner).getDefaultViewModelProviderFactory() : ViewModelProvider.NewInstanceFactory.getInstance()));
    }

    public ViewModelProvider(@NonNull ViewModelStoreOwner owner, @NonNull Factory factory) {
        this(owner.getViewModelStore(), factory);
    }

    public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) {
        this.mFactory = factory;
        this.mViewModelStore = store;
    }

    @NonNull
    @MainThread
    public <T extends ViewModel> T get(@NonNull Class<T> modelClass) {
        String canonicalName = modelClass.getCanonicalName();
        if (canonicalName == null) {
            throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels");
        } else {
            return this.get("androidx.lifecycle.ViewModelProvider.DefaultKey:" + canonicalName, modelClass);
        }
    }

    @NonNull
    @MainThread
    public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> modelClass) {
        ViewModel viewModel = this.mViewModelStore.get(key);
        if (modelClass.isInstance(viewModel)) {
            if (this.mFactory instanceof OnRequeryFactory) {
                ((OnRequeryFactory)this.mFactory).onRequery(viewModel);
            }

            return viewModel;
        } else {
            if (viewModel != null) {
            }

            if (this.mFactory instanceof KeyedFactory) {
                viewModel = ((KeyedFactory)((KeyedFactory)this.mFactory)).create(key, modelClass);
            } else {
                viewModel = this.mFactory.create(modelClass);
            }

            this.mViewModelStore.put(key, viewModel);
            return viewModel;
        }
    }

    public static class AndroidViewModelFactory extends NewInstanceFactory {
        private static AndroidViewModelFactory sInstance;
        private Application mApplication;

        @NonNull
        public static AndroidViewModelFactory getInstance(@NonNull Application application) {
            if (sInstance == null) {
                sInstance = new AndroidViewModelFactory(application);
            }

            return sInstance;
        }

        public AndroidViewModelFactory(@NonNull Application application) {
            this.mApplication = application;
        }

        @NonNull
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            if (AndroidViewModel.class.isAssignableFrom(modelClass)) {
                try {
                    return (ViewModel)modelClass.getConstructor(Application.class).newInstance(this.mApplication);
                } catch (NoSuchMethodException var3) {
                    throw new RuntimeException("Cannot create an instance of " + modelClass, var3);
                } catch (IllegalAccessException var4) {
                    throw new RuntimeException("Cannot create an instance of " + modelClass, var4);
                } catch (InstantiationException var5) {
                    throw new RuntimeException("Cannot create an instance of " + modelClass, var5);
                } catch (InvocationTargetException var6) {
                    throw new RuntimeException("Cannot create an instance of " + modelClass, var6);
                }
            } else {
                return super.create(modelClass);
            }
        }
    }

    public static class NewInstanceFactory implements Factory {
        private static NewInstanceFactory sInstance;

        public NewInstanceFactory() {
        }

        @NonNull
        static NewInstanceFactory getInstance() {
            if (sInstance == null) {
                sInstance = new NewInstanceFactory();
            }

            return sInstance;
        }

        @NonNull
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            try {
                return (ViewModel)modelClass.newInstance();
            } catch (InstantiationException var3) {
                throw new RuntimeException("Cannot create an instance of " + modelClass, var3);
            } catch (IllegalAccessException var4) {
                throw new RuntimeException("Cannot create an instance of " + modelClass, var4);
            }
        }
    }

    abstract static class KeyedFactory extends OnRequeryFactory implements Factory {
        KeyedFactory() {
        }

        @NonNull
        public abstract <T extends ViewModel> T create(@NonNull String var1, @NonNull Class<T> var2);

        @NonNull
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            throw new UnsupportedOperationException("create(String, Class<?>) must be called on implementaions of KeyedFactory");
        }
    }

    static class OnRequeryFactory {
        OnRequeryFactory() {
        }

        void onRequery(@NonNull ViewModel viewModel) {
        }
    }

    public interface Factory {
        @NonNull
        <T extends ViewModel> T create(@NonNull Class<T> var1);
    }
}

ViewModelStoreOwner
public interface ViewModelStoreOwner {
    @NonNull
    ViewModelStore getViewModelStore();
}

ComponentActivity
NonConfigurationInstances
  @Nullable
    public final Object onRetainNonConfigurationInstance() {
        Object custom = this.onRetainCustomNonConfigurationInstance();
        ViewModelStore viewModelStore = this.mViewModelStore;
        NonConfigurationInstances nci;
        if (viewModelStore == null) {
            nci = (NonConfigurationInstances)this.getLastNonConfigurationInstance();
            if (nci != null) {
                viewModelStore = nci.viewModelStore;
            }
        }

        if (viewModelStore == null && custom == null) {
            return null;
        } else {
            nci = new NonConfigurationInstances();
            nci.custom = custom;
            nci.viewModelStore = viewModelStore;
            return nci;
        }
    }
     @NonNull
    public ViewModelStore getViewModelStore() {
        if (this.getApplication() == null) {
            throw new IllegalStateException("Your activity is not yet attached to the Application instance. You can't request ViewModel before onCreate call.");
        } else {
            if (this.mViewModelStore == null) {
                NonConfigurationInstances nc = (NonConfigurationInstances)this.getLastNonConfigurationInstance();
                if (nc != null) {
                    this.mViewModelStore = nc.viewModelStore;
                }

                if (this.mViewModelStore == null) {
                    this.mViewModelStore = new ViewModelStore();
                }
            }

            return this.mViewModelStore;
        }
    }
     @NonNull
    public ViewModelProvider.Factory getDefaultViewModelProviderFactory() {
        if (this.getApplication() == null) {
            throw new IllegalStateException("");
        } else {
            if (this.mDefaultFactory == null) {
                this.mDefaultFactory = new SavedStateViewModelFactory(this.getApplication(), this, this.getIntent() != null ? this.getIntent().getExtras() : null);
            }

            return this.mDefaultFactory;
        }
    }
   static final class NonConfigurationInstances {
        Object custom;
        ViewModelStore viewModelStore;

        NonConfigurationInstances() {
        }
    }
Activity attach 恢复
NonConfigurationInstances
	
 @UnsupportedAppUsage
    final void attach(Context context, ActivityThread aThread,
            Instrumentation instr, IBinder token, int ident,
            Application application, Intent intent, ActivityInfo info,
            CharSequence title, Activity parent, String id,
            NonConfigurationInstances lastNonConfigurationInstances,
            Configuration config, String referrer, IVoiceInteractor voiceInteractor,
            Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken) {
              mLastNonConfigurationInstances = lastNonConfigurationInstances;
            }

 NonConfigurationInstances retainNonConfigurationInstances() {
        Object activity = onRetainNonConfigurationInstance();
        NonConfigurationInstances nci = new NonConfigurationInstances();
        nci.activity = activity;
        nci.children = children;
        nci.fragments = fragments;
        nci.loaders = loaders;
        if (mVoiceInteractor != null) {
            mVoiceInteractor.retainInstance();
            nci.voiceInteractor = mVoiceInteractor;
        }
        return nci;
    }
    static final class NonConfigurationInstances {
        Object activity;
        HashMap<String, Object> children;
        FragmentManagerNonConfig fragments;
        ArrayMap<String, LoaderManager> loaders;
        VoiceInteractor voiceInteractor;
    }

ActivityThread performDestroyActivity 保存
   @UnsupportedAppUsage
    final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
	
   ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
            int configChanges, boolean getNonConfigInstance, String reason) {
        ActivityClientRecord r = mActivities.get(token);
        Class<? extends Activity> activityClass = null;
        if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
        if (r != null) {
            activityClass = r.activity.getClass();
            r.activity.mConfigChangeFlags |= configChanges;
            if (finishing) {
                r.activity.mFinished = true;
            }
            performPauseActivityIfNeeded(r, "destroy");
            if (getNonConfigInstance) {
                try {
                    r.lastNonConfigurationInstances
                            = r.activity.retainNonConfigurationInstances();
                } catch (Exception e) {
                    if (!mInstrumentation.onException(r.activity, e)) {
                        throw new RuntimeException(
                                "Unable to retain activity "
                                + r.intent.getComponent().toShortString()
                                + ": " + e.toString(), e);
                    }
                }
            }
         
        return r;
    }


ActivityClientRecord
	public static final class ActivityClientRecord {
        @UnsupportedAppUsage
        public IBinder token;
        Activity.NonConfigurationInstances lastNonConfigurationInstances;
        
 }

到了这里,关于LiveData和ViewModel源码学习的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android JetPack深入分析LiveData源码

    Google Android开发者 LiveData概览 在了解 LiveData 源码之前,建议先了解 LifeCycle 相关知识,请参考:Android JetPack深入分析Lifecycle源码 定义 从定义中,我们可以大概知道: LiveData 是一个数据存储类,能够感知 Activity 、 Fragment 等组件的生命周期,并且支持对它存储的数据进行观察

    2024年02月12日
    浏览(42)
  • Android JetPack 深入分析ViewModel源码

    本文主要分析 ViewModel 相关源码,相关使用不再赘述,可参考Android ViewModel使用; ViewModel 概览 Google官方给的 ViewModel 定义如下: 定义主要提到两个关键点: 生命周期 上图是Google官网提供的 ViewModel生命周期图示 ,可以看到 ViewModel 的生命周期是从 onCreate 创建到 完成并销毁

    2024年02月11日
    浏览(41)
  • Android Jetpack 从使用到源码深耕【LiveData 从实践到原理 】(二)

    上文,我们就一个实例需求,引入了LiveData进行了实现,大家通过前后的编码实现方案对比,可以感受到LiveData的好用。不由的为jetpack组件的开发者点赞。 Android Jetpack 从使用到源码深耕【LiveData 从实践到原理 】(一) Android Jetpack 从使用到源码深耕【LiveData 从实践到原理 】

    2024年02月08日
    浏览(35)
  • Android Jetpack 从使用到源码深耕【LiveData 从实践到原理 】(四)

    前面,经过LiveData的使用、自我编程实现LiveData框架、Jetpack LiveData源码探索几节的学习,相信大家都收益匪浅。 Android Jetpack 从使用到源码深耕【LiveData 从实践到原理 】(一) Android Jetpack 从使用到源码深耕【LiveData 从实践到原理 】(二) Android Jetpack 从使用到源码深耕【Li

    2024年02月04日
    浏览(41)
  • Android Jetpack 从使用到源码深耕【LiveData 从实践到原理 】(三)

    之前几篇文章,我们通过一个简单的实例需求,将LiveData引入了进来,从而知道了使用它的好处。然后我们通过对其原理的猜想,实现方案的步步探索,自己通过编码实现了LiveData框架。 Android Jetpack 从使用到源码深耕【LiveData 从实践到原理 】(一) Android Jetpack 从使用到源码

    2024年02月04日
    浏览(39)
  • Android Jetpack 从使用到源码深耕【ViewModel从实践到原理 】(三)

    上文,我们通过简单的ViewModel使用源码入手,对其源码进行阅读,原理进行了简单总结,简单来说,ViewModel是通过Activity的onRetainNonConfigurationInstance 与 getLastNonConfigurationInstance的自动调用,实现了 ViewModel数据的存储和恢复,数据存储在ViewModelStore的map中,更加明确的话,应该是

    2024年02月04日
    浏览(121)
  • Android Jetpack 从使用到源码深耕【ViewModel从实践到原理 】(二)

    上文,我们对ViewModel的引入背景、使用、进行了实例编码、总结分析,本文开始,我们对ViewModel的实现源码,进行探索、总结。 Android Jetpack 从使用到源码深耕【ViewModel从实践到原理 】(一) Android Jetpack 从使用到源码深耕【ViewModel从实践到原理 】(二) Android Jetpack 从使用

    2024年02月04日
    浏览(48)
  • Android Jetpack 从使用到源码深耕【ViewModel从实践到原理 】(一)

    五一期间,我们来一起学习总结一下Jetpack的ViewModel组件,从使用、源码、经验来总结分析。 Android Jetpack 从使用到源码深耕【ViewModel从实践到原理 】(一) Android Jetpack 从使用到源码深耕【ViewModel从实践到原理 】(二) Android Jetpack 从使用到源码深耕【ViewModel从实践到原理

    2024年02月01日
    浏览(47)
  • Android架构组件LiveData

    LiveData LiveData是基于观察者模式创建的,其中,LiveData是被观察者,观察者通过注册方法,监听被观察者的数据变化。LiveData在数据发生变化的时候,会通知观察者。 LiveData是一个容器,存放数据的容器,它的数据变化可以被监听,也就是LiveData是一个被观察者,如下,创建了一

    2024年02月13日
    浏览(34)
  • Android的ViewModel

    在Compose的学习中,我们在可组合函数中使用rememberSaveable ​​​​​​​ 保存应用数据,但这可能意味着将逻辑保留在可组合函数中或附近。随着应用体量不断变大,您应将数据和逻辑从可组合函数中移出。 而在之前的应用架构学习中,我们接触到了MVVM架构,他将应用数据

    2024年02月21日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包