Waiting for database information before displaying list - java

I have a dropdown list in a fragment that I access by referencing a ViewModel I initialize in the main activity, said ViewModel retrieving data from a mySQL database using Volley. This normally works as almost always the data has been successfully retrieved by the time the user opens the fragment requiring it, however, if I update the database and refresh my ViewModel's contents this is almost never the case (upon update I immediately return to the fragment where I need the data)
what I'd like is to be able to show a loading prompt until confirmation of successful retrieval of the data, but all resources I can find on this use the now deprecated AsyncTask, so I'm not sure how best to approach this.

Related

My data gets null when app is relaunched after being at background for some time

I am using retrofit to fetch some data from api and using Live data to observe the change in data but whenever I press the home button and start using different apps and after sometime when I reopen my app, data gets null and on pressing back it goes to previous fragment where also it needs to fetch data from api but it doesn't fetch that also but when I press back button again it goes to home page and it fetches the data . I don't know why this is happening and what can be its solution.
Just avoid this issue entirely by storing your values using SharedPreferences inside the onPause and onDestroy method overrides.
This way, data will still prevail even if user accidentally clears all running apps.
This will provide data retention stability to your app.

Passing Data to an Activity without Starting the Receiving Activity (Android)

I am making a contacts app that presents the user with the following Buttons, "Create New Contact", "Edit Contact", "Delete Contact", "Display Contact" and "Finish".
There is a Contacts List activity which must be entered/displayed when clicking on Edit, Delete, or Display. When creating a contact in the "Create New Contact" Activity, I would like to send the Contact object (it implements Parcelable) to an ArrayList within the Contacts List Activity WITHOUT starting it, once the Contact is added, I would like to return to MainActivity.
I am unsure how to go about this as using Intents results in going straight to the activity and I am also unsure how to cause the ArrayList within the Contacts List Activity to save.
Any suggestions? I do not need exact code but I am unsure what direction to go in to accomplish this task.
In my opinion the data model should live in the context of the app and not in the context of an activity. Then the data is accessible from all activities. Now the only object you need to pass by intent is the id of a data set.
This general principle also enables what you want to do, start a new activity, do some data manipulation and send a new intend to the next activity without displaying the previous one at all.
There is a logical problem: if there is not a Activity instance, why do you need to send data to it(even not existed)?
Alternatively, if you'd like the data existed within the whole user lifecycle, it needs to save edited data(ADD,EDIT,REMOVE) in somewhere like local storage or Database. And fetch data(then parse it to specific object) from persistent layer when start the presenter activity (where received data and presented them to user).

How to maintain state of object throughout the app and change the UI at the same time

I'm developing an app having HomeFragment which displays posts of the user you are following. Now each post is having Like and Comment option and when you click on user profile pic or name, it will redirect you to his/her profile which again displays all his/her posts. Now again if you click on one of the post it will redirect you to post detail screen which implements ViewPager where you can change the post by swapping left or right.
My question is every post has Like and Comment option with Like and Comment count, if you like any one post then it should reflect at all the activities(Home, User profile and post detail).
Currently I'm handling all these using startActivityForResult and onActivityResult (eg: if you like post at position 2 and when you click back button then I'm sending that position with new count and notifying that particular position with new like count at HomeFragment) which is very confusing and I think is not the proper solution.
Is there any Design Pattern or any other mechanism to handle this?
If you didn't get my question then let me know, I'll elaborate it more.
Thank you.
My approach in my apps:
In activity onCreate I start Intent service to load data from server. In Intent service data are loaded from server, then parsed and pushed to local database (I use super fast Realm database). When intent service finished, UI is notified to refresh it's content. When you return to previous activity onResume is called where I reload data from database and refresh UI.
To sum it up, UI works only with local database where all data are stored. When new data are loaded on background thread, this data are also pushed to local database. So no need to send loaded data (only needed object ID's) between activities because database contains always up to date data.
You can create singltone class which will hold every posts likes and comments.
Every time you open activity/fragment you pull data from this class.
Every time user adds like or comment you push data to this class.
I also recomend you to take a look at MVP pattern

Storing data across Fragment and Activity life cycles in android

I need to store an array of Custom Objects to populate a ListView in android. I fetch the data to populate the ListView in a Fragment that is a part of NavigationDrawer (The Fragments are replaced frequently). The next time I start the Fragment, data is fetched again. I want this data to be persistent even when I go to another Activity and come back.
I'm not considering using a database as I don't need the data to be restored when the user restarts the app. I just need the data as long as the user is actively using the app.
I've considered the following methods:
Singleton class
Subclassing the Application class
Saving the Instance state in the Fragment/Activity
Is it fine to store an array of 2000 objects in the Application subclass? Is there a better methos to do this?
You shouldn't consider using the Singleton Class, if you want your data to be persistent.The best Android practice to store persistent data are :
1.Application Preferences
2.Files
3.contentProviders
4.SQLite DB
You can read more about it here : http://developer.android.com/guide/faq/framework.html

How to check for updates while app is closed in Android?

So here is my case. I have one main activity and when I start it I fetch some data from a local server via JSON in an AsyncTask and store that data in an ArrayList.
Also when I launch the app I start a service which is polling data from the server every X seconds. I use an intent to send that arrayList to the service so it can compare and see if there are any changes. The service is supposed to run all the time even if the app is closed.
All that is managed so far but I did a big mistake not storing the initially fetched data from the activity in SQLite or something like that.
Do you think its a good idea to do that ? Using SQLite for checking for updates ?
I also forgot to mention that if I close the app and when the service tries to compare it gets a nullpointer because I assume the activity does not exist anymore once I stop it.
I am open to suggestions.
If your data is simple enough, just try storing it in SharedPreferences. It'll avoid a lot of SQL headaches.
You can just fetch that data from the service instead of using Intents as well. Keep it simple!

Categories

Resources