Design Pattern: Similar Fragments with different data sources - java

so I have a design pattern related question. In my Android app I have two very similar Fragments (they use the same layout file) that populate the layout with given data. The only difference is the data source. One Fragment reads the data from a remote database the other one from a local database.
I could think of three options to implement this
Retrieve the required data before creating the fragments and then passing the data objects via the fragment's constructor.
Create an interface DataRetriever with method retrieve() and to implementations RemoteDataRetriever, LocalDataRetriever and pass this to the fragment.
Create to seperate implementations of the fragment or a base class that inflates the layout and two child classes that retrieve the data from different location.
What would you recommend? Or is there another approach I overlooked?
Thanks in advance.
Kind regards.

Related

How can I store and update a common (temporary) data between activities and fragments in Android?

i have an application with multiple fragments and activities. I'm wondering if I can create a container class between fragments. (I want to save with set and pull with get)
For example, I will save the gender information of the user in the FragmentA class, and then I will use this information when I switch to FragmentB. In the same way; I will get the user's age, height, weight in FragmentC class. and then I will access this information in the FragmentE (Final) class and save it to the Room database. How can I do this? Could you please give an example in Java?
Note: I'm using Shared Preferencens but I want to look for a different method as I don't know if it works asynchronously or synchronously. For example, can we create such a container with ViewModel LiveData, store the data and access it from anywhere?
Shared ViewModel in Android to communicate with other fragments. You can save your all data in this SharedViewModel and acsess it in all the fragments.
Please follow this link https://www.geeksforgeeks.org/shared-viewmodel-in-android/
Here they have used two fragment which act as sender and receiver similarly you can create for your multiple fragments
//Java Implementation of SharedViewModel
Sharing data between fragments using new architecture component ViewModel

ViewModel Data Lost When BackPress

firstly I want to say I am sorry. I am newbie in MVVM. I want to know how to retain the data in viewmodel ??
For example I have followed this codelab tutorial https://codelabs.developers.google.com/codelabs/android-lifecycles/#0.
I try to kill the apps then go back into the apps but the data is not saved .Why?
I tried to make new activity by intent it. I ln new activity I implement the same code as statelifecyle. But why when I backpressed and try to enter back the newactivity the data is not saved ?
To answer your questions:
Data in ViewModel is only persisted throughout the lifecycle of your activity. So if your app dies, your data is not saved. If you want it to persist, consider integrating an off-line data persistence library like Room or you can also use SharedPreferences depending on your use case.
According to this post: Android LiveData - how to reuse the same ViewModel on different activities?
When you call ViewModelProviders.of(this), you actually create/retain
a ViewModelStore which is bound to this, so different Activities have
different ViewModelStore and each ViewModelStore creates a different
instance of a ViewModel using a given factory, so you can not have the
same instance of a ViewModel in different ViewModelStores
In other words, different activities cannot share a single ViewModel. So if you want to switch pages while retaining data in your ViewModel, consider using fragments inside your activity instead.

GWT MVP: pass data from view to presenter

I am looking at this GWT MVP tutorial:
http://www.gwtproject.org/articles/mvp-architecture.html#binding
The data is passed from the presenter to the view using this method:
display.setData(data);
where data is ArrayList<String>
How would I pass the data between view and presenter if I had something like a SelectBox in my view where I can select multiple values that I probably populate from a database?
I would store the SelectBox values probably in a HashMap.
Should I implement a second method to pass this to the presenter or should I try to keep the communication between view and presenter down to just one method?
In essence: How can views and presenters communicate through a simple method that ideally takes only one parameter, taking into consideration that sometimes I need to pass a simple string which represents an e-mail address and sometimes something more complex such as the values of a SelectBox?
You can add as many methods as you like to make your communication between your VIEWS & PRESENTERS! The key point lies in the choice of your methods and the implementation, because, debugging a GWT code-base is a nightmare!
There are approaches that you could choose from:
You can have a DISPLAY interface of your PRESENTER be implemented on your VIEW
You could generate an event (on MULTI-SELECT of Values from your SelectBox on VIEW), implement a corresponding handler on your PRESENTER to overcome multiple methods that need to be put in your DISPLAY interface!
Choose your option, based on your requirement and also on the complexity of it! My suggestion is to have an event, since the same piece of code can be reused somewhere else as well!

Where I should store SQLite database reference?

I'm creating simple application that uses JSON format and SQLite database to store parsed information into that. I'm aiming to support both normal devices and tablets, so I'm using Android Fragment API.
My problem is that I'm not sure where I should store the reference to my database, at this moment I have main activity that incorporates two fragments - list fragment and details fragment (both of them are adjusted for tablets and normal phones).
I need to fill mentioned list with information from database and in future I want to implement additional search function so reference to database must be shared by almost all fragments/activities.
In addition I have special class(Util) that stores HTTP object and JSON parser. Is it good idea to add static database reference to that class and create new instance of it in seperate thread (initialization block) ? Or I should consider create it in MainActivitity and create getter and setter ? My MainActivity implements appropriate interfaces from list fragment so I'd need to cast it.

Android: Using observer pattern with Views (possibly MVC, MVVM related)

I've spent some hours reading various questions and answers regarding implementing the various MVC-type patterns in Android. I've seen a few code examples posted in various blogs. I would, however, still appreciate some ideas and opinions on what I am trying to achieve. Specifically, I'd like to work out the best code mechanism to inform a single View, or a group of Views, that a particular item of data has been changed.
My application is quite simply one which obtains measurement data from a hardware device via Bluetooth, and displays and logs that data. At present, I have a Service which takes care of Bluetooth communications and background logging. I have a 'global' data store class that is an extension of Application.
As measurement data is polled from the external device, the measurement data (which is in reality about thirty bytes of data) is updated in the data store object (which, in MVC terms, I'm guessing is the 'model').
At any time, only a small subset of that data will be displayed by UI Views. Typically, a given View will only be interested in representing one particular byte of measurement data. As the user moves to different Activity classes, other Views will be displayed which would display a different subset of that data.
So to get to the point, I'm trying to choose the best way to cause invalidate() to be invoked on interested Views when a given data item is changed.
The options seem to be:
Make use of the existing Observer class, and related classes.
Kind of 'roll my own' observer pattern, by creating my own register() and unregister() functions in the data model. Observer Views would be held in an ArrayList (or perhaps a more complex arrangement of one observer List per data item). I'd loop through this ArrayList each time data are updated and call invalidate() (or postInvalidate() of course, depending on my threading arrangement).
Are there any reasons why I should use one of the above over the other? And is there any other 'observer' mechanism I should consider?
Many views in Android are backed by some subclass of BaseAdapter which has a method notifyDataSetChanged() which instructs the view to refresh itself. If you are using a view (such as ListView or GridView or any descendent of AdapterView) then it is backed by a BaseAdapter and you can simply update that Adapter and the view will refresh itself.
I guess this means, I vote that you use the built-in observer pattern. If you are using a custom view then obviously this won't work and you would have to use a custom method of refreshing anyway.
Another Option would be to use the Android Intent framework. When new data is received in the service set the data to the universal model and broadcast an intent that the data has been updated using the Context.broadcastIntent(Intent) method. Any view that is interested in that data would register and unregister receivers using the Context.RegisterReceiver(Receiver) and Context.unregisterReceiver(Receiver) methods. From there the view would retrieve the data from the universal model and update the view accordingly.
I think this might be what the observer pattern in Android.Lifecycle.Observer package is doing behind the scenes.

Categories

Resources