Let me directly say that this is rather a “structural” question, and I am not asking anyone to write code; I'm just trying to figure out how I should structure my application.
I am using Android DrawerLayout
/ NavigationView
for my application. This means that it MainActivity
is the host for all my fragments.
I currently have three fragments (actually it is a lot more, but they are more or less exactly similar to these three fragments, only for different data sets).
- Listfragment
- DetailFragment
- EditFragment (used to add and edit)
On mine ListFragment
I have (surprise!) A list of items. This is a collection LiveData
on SharedViewModel
(linked to a MainActivity
life cycle). When an element is being listened, I pass the event through MainActivity
using the interface listener.
MainActivity
then loads DetailFragment
. In the same call, I load an instance SharedViewModel
(again bound to MainActivity
). I installed SharedViewModel.selectedItem
as a knocked item. Then in the function, DetailFragment
onCreate
I get the selected item through ViewModelProviders.of(activity).get(SharedViewModel::class.java).selectedItem
.
There DetailFragment
is an edit button on. This goes through more or less the same procedure described above, but instead loads EditFragment
. When the edited / added item is saved, I add / replace the item in the collection SharedViewModel
through the interface listener MainActivity
.
, . -, , , MainActivity
( , ). , MainActivity
- , .
, , ListFragmentViewModel
, ListFragment
. DetailFragmentViewModel
, EditFragmentViewModel
..
, , . , , EditFragment
. ListFragmentViewModel
? ListFragment
back-stack, viewmodel , , . , , ( , , DetailFragment
ListFragment
?), , .
, (, DetailFragmentViewModel
.)
, . - ?