android-lifecycle

Reacting to activity lifecycle in ViewModel

好久不见. 提交于 2021-02-18 04:39:38
问题 I'm trying to create an app which will use MVVM architecture and there's one thing I quite don't understand. Official Android docs say that's not a good idea to reference activity context in ViewModel's (as ViewModel may outlive activity) so I've started to wonder about usecase when I want to execute some action when my activity is resumed. I know ViewModel's shouldn't do business logic themselves but even if I use some service class (let's say GPSService which has to start and pauseeach time

Reacting to activity lifecycle in ViewModel

人盡茶涼 提交于 2021-02-18 04:39:21
问题 I'm trying to create an app which will use MVVM architecture and there's one thing I quite don't understand. Official Android docs say that's not a good idea to reference activity context in ViewModel's (as ViewModel may outlive activity) so I've started to wonder about usecase when I want to execute some action when my activity is resumed. I know ViewModel's shouldn't do business logic themselves but even if I use some service class (let's say GPSService which has to start and pauseeach time

Reacting to activity lifecycle in ViewModel

人盡茶涼 提交于 2021-02-18 04:38:34
问题 I'm trying to create an app which will use MVVM architecture and there's one thing I quite don't understand. Official Android docs say that's not a good idea to reference activity context in ViewModel's (as ViewModel may outlive activity) so I've started to wonder about usecase when I want to execute some action when my activity is resumed. I know ViewModel's shouldn't do business logic themselves but even if I use some service class (let's say GPSService which has to start and pauseeach time

Reacting to activity lifecycle in ViewModel

白昼怎懂夜的黑 提交于 2021-02-18 04:38:08
问题 I'm trying to create an app which will use MVVM architecture and there's one thing I quite don't understand. Official Android docs say that's not a good idea to reference activity context in ViewModel's (as ViewModel may outlive activity) so I've started to wonder about usecase when I want to execute some action when my activity is resumed. I know ViewModel's shouldn't do business logic themselves but even if I use some service class (let's say GPSService which has to start and pauseeach time

Accessing ViewModel in ParentFragment from ChildFragment using viewModels

时光怂恿深爱的人放手 提交于 2021-01-29 20:30:27
问题 I am trying to access the ParentViewModel for ParentFragment from ChildFragment using viewModels . This is my code. // In ParentFragment class ParentFragment : Fragment() { val parentViewModel: ParentViewModel by viewModels { ParentViewModelFactory(getRepository()) } ... } // In ChildFragment class ChildFragment : Fragment() { val parentViewModel: ParentViewModel by viewModels( { requireParentFragment() } ) ... } However, this code only works when I setup a LiveData variable in

onActivityCreated deprecation : how to add fragments as observers of MainActivity using NavigationComponent

风流意气都作罢 提交于 2021-01-29 12:37:47
问题 I just saw that onActivityCreated() is going to be deprecated in future. I try to implement LifecycleOwner and LifecycleObserver pattern but I'm not quite sure about what I'm doing here. I'm using NavigationComponent, which meens : I have a MainActivity I have a MainFragment, instanciated as the home fragment I have multiple fragments that can be accessed from this home fragment For some reasons I need to know when activity is created from all of these fragments (MainFragment and sub

viewModelScope not cancelled

拜拜、爱过 提交于 2021-01-27 05:56:20
问题 After watching Sean's explanation on Android (Google I/O'19) I've tried the same: init{ viewModelScope.launch { Timber.i("coroutine awake") while (true){ delay(2_000) Timber.i("another round trip") } } } Unfortunately onCleared it's called when the activity is killed but not when it's put in background ("when we move away from the Activity...", background is "moving away" imho ^^). And I get the following output: > ---- Activity in Foreground > 12:41:10.195 TEST: coroutine awake > 12:41:12

Is onActivityCreated() called after onViewCreated() in a Fragment?

旧城冷巷雨未停 提交于 2021-01-27 03:54:21
问题 I have two ViewModels. One is used from the Fragment only and the other one is a shared ViewModel from the Activity. Fragment: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); viewModel = ViewModelProviders.of(this).get(FragmentViewModel.class); ... } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); activityViewModel = ViewModelProviders.of(getActivity()).get

Fragment lifecycle events in LifeCycleAware Fragment

て烟熏妆下的殇ゞ 提交于 2020-12-08 05:34:12
问题 I have a lifecycle aware fragment and a LifecycleObserver class public class MyFragment extends Fragment { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); new MyObserver(this); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.activity_main, container, false); } } Following is my Observer class where it logs all the

LifecycleRegistry not getting initialized, in the BaseActivity for MVP architecture

扶醉桌前 提交于 2020-11-29 11:00:08
问题 I have the few Base classes defined for my Activity and Fragment, Presenter, and ViewHolder, and ViewModel, and an interface and Presenter being defined, All other activities inheriting these base classes, and In my BaseActivity class and BaseFragmentClass, i am creating lifecycleRegistry object and initializing it, but when the App is launched it crashes with error getLifecycle() returned null in ComponentActivity's constructor. Please make sure you are lazily constructing your Lifecycle in