android / codelab-android-room-with-a-view

Apache License 2.0
752 stars 490 forks source link

Should be using 2 fragments with an activity-scoped shared ViewModel instead of passing stuff around in `onActivityResult` #47

Open Zhuinden opened 4 years ago

Zhuinden commented 4 years ago

Using onActivityResult in https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin/index.html?index=..%2F..index#13 bypasses the original premise of ViewModel described in https://codelabs.developers.google.com/codelabs/android-lifecycles/#5 and is therefore not the best way to represent how to use AAC.

Especially considering that the Android Persistence Codelabs was deprecated in favor of this codelabs (https://github.com/googlecodelabs/android-persistence/issues/27) and should represent Best Practices.

Currently, this Codelabs does not actually represent the AAC best practices, and this should be fixed. With this flaw, I can't in good conscience link this to any beginners at the moment (without adding the footnote to "beware the 13-14th steps").

DarkOhms commented 2 years ago

what should I do instead?

Zhuinden commented 2 years ago

what should I do instead?

in this day and age where Jetpack Navigation 2.2.0+ exists, you'd want to have a shared <navigation tag to which you can use as a scope using the NavBackStackEntry, that way you can share state/data between the two screens via a NavGraph-scoped ViewModel

DarkOhms commented 2 years ago

I'm confused. Why do I need state data? I'm saving to a Room database, correct? The database is under observation in the MainActivity. I get the feeling that I want to know what this Jetpack Navigation stuff is all about but right now I have a specific task to perform, add new information to my database, and I don't know if I want or need to learn a brand new paradigm. My research has led me to exploring Fragments and abandoning the use of another Activity. This seems to be the direction that Google went with things but I have no idea what that has to do with navigation tabs. I'm sold on the idea that adding to the database doesn't necessarily merit it's own Activity.

What I really want is something like a Dialog to pop up and take the information and send it to the database. I was hoping to find that Dialog inherits from Fragment or something but I learn nothing about it's relationship to Fragments and Activities because it inherits from Object lol! I don't know why scope is a concern if I am only adding one thing at a time and the underlying calls have their own coroutine scope.

I just found DialogFragment. Maybe that's what I'm looking for.

Zhuinden commented 2 years ago

you can use a DialogFragment and return a view in onCreateView and it would show on top of your current window, yeah