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.
Related
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
My app initially makes a request for a list of objects from a server. These objects are currently kept in memory as an ArrayList<MyObject>. However, I want these objects to be passed through multiple activities before the user terminates the flow by pressing a button. I could make the ArrayList serializable and pass it through Intent extras. But I could also store MyObject(s) in a SQLite database and access/modify them in any Activity without having to go though intents. I was wondering what the norm is to accomplish this.
EDIT: forgot to mention that all the values would be deleted once the user terminates the flow.
SQLite is not the best way to go in your case since you don't need the data to be persistent after you close the app. It will just slow your app having to store and retrieve all entries on every activity transition. You can do one of the following instead:
Pass Serializable the way you described. Might be slower than the other alternatives though
Make MyObject implement Parcelable and use [intent.putParcelableArrayListExtra()](https://developer.android.com/reference/android/content/Intent.html#putParcelableArrayListExtra(java.lang.String, java.util.ArrayList))
Extend Application and load the list from the network in your Application.onCreate() and call getList() from activities that need it. That way you load it once and you don't need to pass it between different activities.
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.
I am trying to save some data using XML in android to save into the devices internal storage.
I want to be sure the app is written by the open/closed and single responsibility -principle.
(So i could easly switch between methods of storage)
-domain
|---db
|---|---appReader
|---|---|---AppReader.java
|---|---|---XMLAppReader.java
|---|---appWriter
|---|---|---AppWriter.java
|---|---|---XMLAppWriter.java
|---DBFacade.java
|---DBFacadeImpl.java
-MainActivity.java
This is my current structure,...
The problem i am facing now, is that in every tutorial about android and xml they use the function openFileOutput(filename,Context.Mode_Append) I guess this is a function from Activity superclass ?
How would i have to initialise my FileOutputStream and still keep my open/closed and single responsibility principle intact ?
Should i pass an Activity instance to my XMLAppWriter constructor ?
Doesn't this undermine the Single responsibility principle ?
Instead of pass activity instance as parameter, you can use application's context method, which doesn't violate single responsibility principle.
getApplicationContext().openFileOutput(name, mode);
I am trying to store a set of objects in an ArrayList and access them in a helper class. I am using Vaadin and a view navigator to navigate from one view that takes user input and navigate to another view that displays some of it on a graph. The views are helper classes. The ArrayList of these objects was outside of the helper classes and I have tried changing the modifiers of the ArrayList (I tried static and final) and I tried referencing the variable from inside the helper classes by using the general variable name as well as the OuterClass.variableName. The reason I am storing it as a variable and not in a database (as seems to be the norm for Vaadin CRUDs) is that my object has lists of other objects inside of it and I could not figure out how to get a flexible input on the UI or the right container for it (I haven't worked with Java in years and I am new to vaadin). I should add that there are no syntax errors, but the ArrayList is always empty.
In essence, the problem I am having is that I can't create an object in one helper class view (object made from user input), store it, and be able to read it from another helper class view (to display the data graphically).
Other then the answer above You can use session attributes of Vaadin UI.
UI.getCurrent().getSession().setAttribute("button", new Button("Button"));
Button b = (Button) UI.getCurrent().getSession().getAttribute("button");
Other possible answer are for e.g. events - an NavigateToViewEvent that sends data to the chosen view.
You have a main class which extends the UI class.
This one is where you could add properties to store your per-user/instance data structures.
You can access the UI class from almost anywhere in your vaadin application.
Via UI.getInstance() (and the cast to your main class) you have access to everything inside this.