Kelin-Hong / MVVMLight

A toolkit help to build Android MVVM Application
1.85k stars 333 forks source link

App crash when activity has been CG #12

Open ryotw opened 7 years ago

ryotw commented 7 years ago

I sometimes have the similar problem in my project(Fragment dependent with activity,but activity has been GC by system). I have done lot of research by this issue, but can't find the best way to solve it. Hope to discuss these issue with you,thanks a lot!

Error log: mlight.zhihu E/AndroidRuntime: FATAL EXCEPTION: main Process: com.kelin.mvvmlight.zhihu, PID: 32108 java.lang.RuntimeException: Unable to stop activity {com.kelin.mvvmlight.zhihu/com.kelin.mvvmlight.zhihu.newsdetail.NewsDetailActivity}: rx.exceptions.OnErrorNotImplementedException: Sequence contains no elements at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3762) at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3819) at android.app.ActivityThread.access$1200(ActivityThread.java:150) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1423) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:168) at android.app.ActivityThread.main(ActivityThread.java:5845) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) Caused by: rx.exceptions.OnErrorNotImplementedException: Sequence contains no elements at rx.Observable$27.onError(Observable.java:8139) at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:157) at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120) at rx.internal.operators.OperatorDoAfterTerminate$1.onError(OperatorDoAfterTerminate.java:54) at rx.internal.operators.OperatorDoOnEach$1.onError(OperatorDoOnEach.java:71) at rx.internal.operators.OperatorSingle$ParentSubscriber.onCompleted(OperatorSingle.java:131) at rx.internal.operators.OperatorTakeLastOne$ParentSubscriber.onCompleted(OperatorTakeLastOne.java:107) at rx.internal.operators.OperatorScan$2.onCompleted(OperatorScan.java:124) at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop(OperatorMerge.java:617) at rx.internal.operators.OperatorMerge$MergeSubscriber.emit(OperatorMerge.java:529) at rx.internal.operators.OperatorMerge$InnerSubscriber.onCompleted(OperatorMerge.java:818) at rx.internal.operators.OperatorMap$1.onCompleted(OperatorMap.java:43) at rx.internal.operators.OperatorFilter$1.onCompleted(OperatorFilter.java:42) at rx.internal.operators.OnSubscribeRefCount$2.onCompleted(OnSubscribeRefCount.java:129) at rx.internal.operators.OperatorPublish$PublishSubscriber.checkTerminated(OperatorPublish.java:425) at rx.internal.operators.OperatorPublish$PublishSubscriber.dispatch(OperatorPublish.java:507) at rx.internal.operators.OperatorPublish$PublishSubscriber.onCompleted(OperatorPublish.java:307) at rx.internal.operators.OperatorMaterialize$ParentSubscriber.drain(OperatorMaterialize.java:149) at rx.internal.operators.OperatorMaterialize$ParentSubscriber.onCompleted(OperatorMaterialize.java:99) at rx.observers.SerializedObserver.onCompleted(SerializedObserver.java:182) at rx.observers.SerializedSubscriber.onCompleted(SerializedSubscriber.java:65) at rx.internal.operators.OperatorTakeUntil$1.onCompleted(OperatorTakeUntil.java:57) at rx.internal.operators.OperatorTakeUntil$2.onCompleted(OperatorTakeUntil.java:72) at rx.internal.operators.OperatorTakeUntil$2.onNext(OperatorTakeUntil.java:82) at rx.internal.operators.OperatorTake$1.onNext(OperatorTake.java:76) at rx.internal.operators.OperatorFilter$1.onNext(OperatorFilter.java:54) at rx.internal.operators.OperatorOnErrorResumeNextViaFunction$4.onNext(OperatorOnErrorResumeNextViaFunction.java:153) at rx.internal.operators.OnSubscribeCombineLatest$LatestCoordinator.drain(OnSubscribeCombineLatest.java:285) at rx.internal.operators.OnSubscribeCombineLatest$LatestCoordinator.combine(OnSubscribeCombineLatest.java:227) at rx.internal.operators.OnSubscribeCombineLatest$CombinerSubscriber.onNext(OnSubscribeCombineLatest.java:382) at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139) at rx.internal.operators.OperatorSkip$1.onNext(OperatorSkip.java:59) at rx.internal.operators.OnSubscribeRefCount$2.onNext(OnSubscribeRefCount.java:124) at rx.internal.operators.OperatorPublish$PublishSubscriber.dispatch(OperatorPublish.java:587) at rx.internal.operators.OperatorPublish$PublishSubscriber.onNext(Opera 02-11 15:28:47.561 1205-2218/? E/ActivityManager: App crashed! Process: com.kelin.mvvmlight.zhihu

kylin17 commented 7 years ago

I think ViewModel needs a lifecycle to perceive the lifecycle of Activity, when activity call onDestroy, needs to unSubscribe the Subscription created by the ViewModel.