I am using MVVM architecture for my PhotoEditorActivity, the activity plays the View role. When user clicks "Pick Image" button, I want to start gallery to pick an image and get back its uri to update an ImageView. I am confused in where to call startActivityForResult().
Call startActivityForResult() from View: By this way, the activity processes everything, ViewModel and Model become redundant.
Call startActivityForResult() from ViewModel and View observes Uri from the ViewModel: I can pass Context to ViewModel to start activity, but I can not receive uri because ViewModel does not have onActivityResult().
Call startActivityForResult() from Model: View pass a Context to ViewModel, ViewModel continue passing the Context to Model. Using this way, I face the same issue with the second way.
Could you please help me how to properly apply MVVM for this business. What should each component contain? Thank you for your attention.
All Android related things are best handled in the View. Specifically making your code loosely coupled depends on what you intended to do with the image / file that you have selected.
If for example you are trying to upload it as a bitmap, then you should create the bitmap in the view and pass it to the view model.
Also, onActivityResult is now depreciated. See the official documentation for it here.
You may also want to look at this answer for an example implementation of it.
Related
I just started with my first Android project. My application contains two fragments that are connected with the navigation controller.
As for now, all I have on my first fragment is only two EditText. However, I realize that when I switch to my other fragment and navigate back, the texts entered have been cleared.
So I would like to know what I can do to save the date and restore them when I switch it back
I have already tried saving them in onSaveInstanceState() as suggested by the majority of people, however, it is not working. So I did a little test, turns out the onSaveInstanceState() is not even being called when I switch the fragment. Instead, onDestroy() and onPause() are being called, ones where people don't recommend to save my data
I have also tried to use ViewModel to save the data but it seems that the data saved into the ViewModel is also gone when I switch back to the original Fragment.
So I am not sure what to do? Thanks!
You should use ViewModel, create your ViewModel by Activity instead of Fragment, inside your Fragment.onViewCreated() method
val myViewModel = ViewModelProvider(activity as ViewModelStoreOwner).get(MyViewModel::class.java)
I‘m just starting with android and i‘m wondering whether every activity need its own view or you can manage the ui in the activity itself ? Also it isn‘t pretty clear to me why the xml file belongs to the activity and not to the view
Any activity that has a UI needs a View. Activities handle a UI by displaying the View you set as the content view via setContentView. Activities have no direct drawing functions.
There are a few situations in which you may have an Activity with no View that just does some processing, but its generally a hack to get somewhere else- like an Activity that acts as a router.
I have an Activity with 3 fragments attached(using viewpager) , Whenever a user in any fragment changes the date range I need to load the relevant data from the DB and show it in the same fragment.
Could anyone guide me to a link or suggest an approach to this please.
I would use Room it enables you to attach an observer (using LiveData), and update the view using the ViewModel so anytime the data is changed in the database the view is updated straight away.
In your case the user input can go straight into the query, and once the data is fetched your view will be updated automatically. I personally have a case where I have 3 fragments in a ViewPager and they display similar data, so I use one ViewModel to fetch it.
Here is a codelab that shows how to implement it. Basically you will have a combination of a ViewModel, LiveData and Room.
I am building an app with a drawer layout similar to the Android Facebook app. I am wondering what the best method for architecture is. Should I have a main activity which is responsible for the action bar, and then have it use fragments to display the content of each menu item, or should I be using one activity to manage the action bar, and then have each menu item kick off entirely separate activities?
I could also imagine building multiple activities, which each have to manage the action bar. This option seems the worst.
You have two architecture options here
MainActivity with Fragments
ParentActivity that handles drawer and lots of Activities that extends this Activity.
I have tried both in different projects and found some things worth sharing.
For me The MainActivity that handles drawer and then using Fragments to fill the display is the best.
You will need to handle callbacks from specific Fragments in your MainActivity and redirect them to the specific Fragment they came from. This is mainly if you use Interfaces in objects lower in the Arcitecture chain since you sometimes need to pass down Activity to certain objects. This generates more code that are not as generic as one might want in top level architecture node.
If you are using a ParentActivity and extending it for each ChildActivity, you can write all specific code in the child, meaning that the toplevel ParentActivity will almost only have generic code.
If you are using the ParentActivity with ChildActivities and you are switching between Activities, you fill get the graphic when an Activity closes and the next opens every time a user switches between navigation objects. If you use Fragments this wont happen as the Fragment will be switched in the background. The user will also experience that the navigation drawer will be closed and recreated each time he clicks on an item there.
Its also unnessecary to recreate the navigation drawer with each click on an item. This is a minus for the ParentActivity approach.
With the ParentActivity approach you will also have to keep track of how the backbutton should function, this will be autoaticly handled for you with Fragments. Also when starting new Activities you have to choose if a new Activity should be created or if the old should be killed etc.
Just my 5c, hopes it helps :)
The best way is to use one Activity with one Fragment per section/view.
Take a look at the design documentation.
Also see the Tutorial and Sample Application. It's fairly straight-forward.
You will have one activity which manages ActionBar, Drawer (ListView!) and Fragment.
Every time it clicks an item in the ListView it updates the fragment with the new view.
If you use different Activities then you should use intents with a very bad effect, use a different activity only if needed (if it's totally unrelated to the current activity maybe?)
Official documentation: http://developer.android.com/training/implementing-navigation/nav-drawer.html
If you got any problem in creating this, online you can found more tutorials but the official is very great.
You should have the activity holding the actionbar & drawer
When using a drawer you should not start new activities from within the drawer but fragment instead
Good post & video about it: https://plus.google.com/u/0/+RomanNurik/posts/3nMVVQzUTjG
another good read: http://www.androiduipatterns.com/2013/05/the-new-navigation-drawer-pattern.html
And finally this is a must see also (check the slides or the video): https://plus.google.com/u/0/+NickButcher/posts/1jeyV2n1ZpM
Everywhere I look online, it seems that I must use an animation to change views dynamically (like slide in, out). Do I have to do this, or can I simple just setContentView() on each new activity?
Thanks!
There's nothing to say you have to use an animation. Just create your intent object, start your activity with startActivity or startActivityForResult, and then call setContentView when it loads. No animation necessary.
HTH.