The idea of using MVVM is that view observes ViewModelchanges and acts. I'm using an Activity which uses 7 Fragments and the navigation between them goes through observing individual changes in the Activity from different Fragments and launching/replacing Fragments accordingly. For instance,Fragment L calls setValue, then as a result the main Activity receives the event and switches to Fragment M and Fragment M calls getValue from the observed MutableLiveData and not directly functioning as a listener to changes. Does that the right structure or should each Fragment observe changes by himself ? What would the right way to handle multiple navigations between multiple Fragments
I don't see anything wrong in what you just said. Fragments should be independent from each other unless they are nested.
If you need communication between fragments that can be done in many ways. The most basic way is to do that through activity. Activity knows how its fragments interact with each other, but fragments remain independent. They can listen to events, no problem here, but make sure you have a 'rule' in your project about how you handle events.
E.g. I prefer having one listener at a time to have the order more predictable. If I want multiple fragments to handle the event, I usually place the handler in the activity and then pass the event to fragments in the exact order I need. Otherwise it might quickly get out of control.
There are other ways too, such as EventBus, BroadcastReceivers or any other event-based mechanism.
I hope that helps. If I didn't answer your question then to answer it more precisely I'd need the question to be more specific.
Related
As the title states, does it considered as a good practice to use a single fragment within an activity to display the content? I began to notice that more and more developers start to use the fragment as an insulation layer to separate the lifecycle logic from UI that activity (sorry, fragment) displays. The most recent example that I stumble on is the architecture blueprints provided by Google developers. They use just one single fragment for UI while the activity handles ViewModel and all the navigation between screens.
So, is this a good practice or just a personal preference? Would you care to share your opinion on the subject?
Using Fragments as your UI is a good practice.
Activity can hold all common logic, while you can use different fragments to show different UI for mobile vs tablet.
If the UI is in Fragment, you can reuse it in multiple activities.
If you have a workflow scenario like Registration flow, using a single activity with multiple fragments will help you out a lot.
Manipulation of fragment backstack is a lot easier than trying to do the same with activities.
Use fragment for display the UI is good,Reason is listed below:-
1.You can create one activity,and display multiple fragment inside that activity.
2.Handling back press is easy as you can override onBackpress() in main activity class and handle back key event from fragment(s) as check its(fragment) visibility and handle event.
3. Reusability of layout.
4. Reusability of fragment.
5. Handling different action menu is very easy for different fragment(s).
I have one very general question, I did not find a concrete answer for my question hence putting it again.
I want to decide between two approaches
Dedicated activities for various various screens and tasks to avoid complexity and issues
Single Activities and multiple fragments for different tasks and user can navigate like Activity holding Fragment A user will navigate to Fragment B, Fragment C , this can be back and forth transaction.
What I want to know?
Is Activity transition is that costly for processor or to achieve simplicity memory overhead is negligible ?
Fragment has overhead of managing life cycle with transition, so what all problem can come with this life cycler management?
How easy is to deal with fragment transaction with saving state of the fragment?
We don't know right now what amount data will be there for fragment to hold.
Well, it totally depends on the application's design, flow and it's navigation.
Here are some benefits of using Single Activity and Multiple Fragments:
Performance fragment transactions are fast than creating new activities.
Navigation Drawer and Toolbar, it's easy to manage with Single activity.
Same Context can be used everywhere.
Fragment's setRetainInstance is very helpful while managing orientation changes.
with it, here comes few drawbacks:
Activity gets really messy with a lots of code.
Handling button backpress is tedious as only Activity can handle that not Fragments.
I personally use Multiple activities with multiple fragments in which I separate activities based on the modules. In same module, submodules can be created in fragments. I found it easy to manage in different scenarios as if application gets closed, reopens, in notifications, orientation changes.
My question is same, till now in my all app, there is only one Activity, and the rests are Fragments. I agree it is hard to maintain Fragment, but using Fragment will increase your Performance.
Suppose, take one example,
I have 10 Activities, in each Activity, I'm calling Async Task to perform some background operations. In each Async Task's onPostExecute() you are updating your UI. But before completing the doInBackground() you switched the Activity and that Activity is destroyed, but remember the doInBackground() is still in progress, and once it is finished, onPostExecute will be called, and in onPostExecute() we are updating the UI, but the Activity is destroyed, so this will create a leak in your app.
But if you are maintaining only one Activity then it will be easy to maintain.
Waiting for others opinion also.
Apart from answers above, few points which I would add which can help in deciding when to use a Fragment and when to use an Activity.
If you're supporting larger screen sizes, fragments are definitely preferred over activities.
When you've some code which you think can be reusable in multiple screens, you can use a fragment which can be reused across different places
A small point to add in support of "single Activity multiple fragments".
Sometimes you may need a component (a banner ad for example) to persist between different pages/screens of your app. If you have multiple Activities, you'll need to recreate that component on each of the Activities. But if you have a single Activity, you just add that component to the Activity layout and can forget about it.
Currently we are have a container activity that hold 2 buttons(next and cancel) along with a content layout that switches depending on the fragment. The fragments follow the mvp pattern but the omain questions seems to how to correctly implement the Next and Cancel button on click events.
Would it be better to make a Presenter for the MainActivity and pass that through to the Fragment and have the fragment work with that for the two button events? Or should the Fragment create new onClicks for the button on each fragment change? My way of thinking seems to go along the lines, the two buttons can be considered part of the fragment view at that moment in time, so the Fragment should be concerned with handling them. But wouldnt this lead to more code writing? Any help would be appreciated.
In your case my choice will be a simple way.
Activity register as listener onto fragment. Fragment handle the button event and call to activity. Each acitivty deal with event action.
As in your description fragment is a plain unit just to introduce next & cancle functions. Implements MVP on this unit cause more code but no income.
Now the question is on the activity side. If there is full business procress then go MVP way, if not just add two functions to respond button is good enought.
There always exchange, MVP more code & complex relationship for extention & team work, plain function call less code & simple procress for small unit & extention unfriendly.
I've been coding android apps for a couple of years now and when I started fragments was the way to go so I've been using them since.
From the beginning I thought the lifecycle and handling of fragments was a mess and tried a couple of different approaches with a single activity or 1 activity per groups of fragments etc.
I was more comfortable with the single activity approach but then ran into problem where to show and hide fragments etc.
I tried to have all logic for showing/hiding a fragment in its own class and then using a model-notifier pattern for pushing out a message containing information about which fragment to show and hide. The last thing I tried was to have a "FragmentService" that gets notified, using EventBus, with a message (DTO) containing information about what fragment to show/hide and a bundle to pass with it.
I'm not totally satisfied with my architecture for managing fragment yet so now I'm curious how you solve this problems? I want every fragment to be loosely coupled from each other and able to start Fragment1 from Fragment2, 3 or 4 etc.
Can this be solved in a centralized and understandable way?
I've recently decided to update my app to support the new fragments feature in honeycomb 3.0.
My Application currently works on a list view that opens different activities depending on which list item is clicked.
Using an adaptation of the code in this tutorial I have created an app that consists of only two activities, but depending on which list item is clicked the second "viewer" activity launches using a different layout xml.
Unfortunately I haven't been able to figure out how to call the old methods that had all the functionality. Should I Import all of my old activities and then call the methods into the viewer activity (I may need some advice on how exactly to do this) or should I just put all the methods directly into the same viewer activity (please consider the size of these methods(which is very large by the way)).
Once everything is working with two activities upfront then it will be a pretty simple task of "fragmenting" the app as demonstrated here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
Thanks
As James has pointed out you will have to move the business logic from your Activities to your Fragments.
To handle events you can create a listener Interface. The CONTAINER activity/ies will implement this interface. As fragments has access to the container activity you will be able to delegate to the container Activity the "logic" for the desired events. For this events the activity will decide whether to launch a new activity, show/hide new fragments or whatever.
I had a similar question, take a look to the question and answer: here
Although I haven't considered that there might be a way to allow multiple fragments to occupy the same space in an activity(If this is the case then please let me know how it's done)
I think its possible to allow multiple fragments to occupy the same space in an activity. Again, take a look to the answer here ... I think the concept/scope of Activity has change a bit and now an Activity can contain different Fragments which every one will allow user to do a single focused thing.
I'm not sure what you mean by "call the old methods that had all the functionality". You'll want to rewrite all of your activity classes as fragments. Check out this tutorial here (it's very concise). Basically, you'll want an activity that consists of a ListFragment and a FrameLayout. Your ListFragment will update the FrameLayout by changing to the appropriate Fragment based on which row was selected.