Currently I try implementing an application with the architecture components provided by android. In terms of only using activities in the UI Package there is no problem with that, but if I implement several fragments which are held by one activity in a fragment container I'll get in trouble. The communication for data requests using fragments should never be called from the fragments itself but from its activity. In this case only the activity will register the needed viewmodel but the lifecycle of each fragment isn't handled properly... how can I face this problem using multiple fragments in one activity requesting data from a server and be lifecycleaware for the fragments even when the activity is calling the viewmodel(s)?
3 Answers
Have you heard of Interface Listener. This will help you in all the ways no matter how much complications are there. If you are still confused do let me know

- 8,577
- 7
- 33
- 60
-
thanks for the answear! But its not about the way how to transfer data from fragments to the activity - the question was how I can handle the lifecycle of each fragment when I only register the viewmodel component in the activity? the observer can't be unregistered when a fragment switches because the activity is still active – ECommerce Jul 31 '17 at 07:57
-
you just need to pass listenerObject of the fragment while requesting a request and return the response to the registered listenerObject – Rahul Khurana Jul 31 '17 at 08:00
@Rahul Khurana
public class UserProfileFragment extends LifecycleFragment {
private static final String UID_KEY = "uid";
private UserProfileViewModel viewModel;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String userId = getArguments().getString(UID_KEY);
viewModel = ViewModelProviders.of(this).get(UserProfileViewModel.class);
viewModel.init(userId);
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.user_profile, container, false);
}
As you can see the fragment is directly communicating with the viewmodel which is a request for data or setting some new data. This action is lifecycle aware but I know that its mandatory to communicate with the datalayer through the activity? This is a code snippet from the official android page

- 469
- 1
- 4
- 9
I´m not sure if I understood you right. You want to load data in your Activity for your Fragment with the lifecycle awareness of your Fragment?
Maybe you can try to access the ViewModel of your Fragment from the Activity to get an FragmentLifecycle aware ViewModel. And than you can query data via this ViewModel.
viewModel = ViewModelProviders.of(YourFragment).get(FragmentViewModel.class);
I´m not sure if this works or if this is a good idea. The google guide only shows it the other way around, accessing the ViewModel from the Activity within the Fragment, to share data between Fragments, or Activity and Fragment.

- 81
- 1
- 10
-
In terms of requesting data via eg a webservice it is wrong to let a fragment handle it. Instead you should use a callback to its activity and the activity is doing this request and then return the requests data to the fragment... – ECommerce Aug 13 '17 at 15:16
-
Using android arch components these kinds of callbacks to the holding activity aren't needed anymore. Google guide tells that you can use webservice directly calling a fragments viewmodel as you mentioned. It handles fragments lifecycle and if the activity gets destroyed the fragment will as well and that is handle by an observer implemented in the fragment. In my solution I still use those callbacks to the holding activity and if the data from all fragments is collected in the activity I call activities viemodel to request data and return it to the next activity. Thanks for ur solution! – ECommerce Aug 13 '17 at 15:16
-
I am glad I could help you! I worried, because I am not sure how this will behave when the Fragment is destroyed or not completely initalized yet. Because than you probably will get a null object. – Simon B. Aug 14 '17 at 08:27