Update Fragment when new element is added to database - android

I'm writing an android app. The app is able to get Push notifications from Parse, and I have been able to take the notification from Parse and put it into an SQLiteDatabase. I have a fragment which, when selected from Navigation Drawer, reads the values from the database and displays them with a Custom Adapter. This is where I need help.
If the user has the Notifications tab currently selected and a new Push notification is received, the notification is put in the database but not displayed until the user navigates away from the Notifications tab and back into the Notifications tab. I'm ok with this, but I'd like the Notifications fragment to update immediately upon Push reception. Is there any way to set a listener on the database to implement this behavior?
Thanks for any replies - I'm able to paste code if it is needed.

I believe a Loader may be what you're looking for. This Udacity course helped me understand how to use Loaders.
Through a Loader/LoaderManager, the onLoadFinished method is called in your fragment/activity when the database is updated. Seeing you are using a SQLite DB, you could use a CursorLoader, which would pass a Cursor in to onLoadFinished and then you can do whatever you want with it, such as send it to an Adapter to display the new data.

Related

Should I store my contacts list in DB or Sharedpreference to avoid running the same task

I have my app such that when entered in a certain activity, I show a list of contacts in my phone( similar to Chat app where you see a list of contacts when trying to start a new chat).
So I just wanted to know about the implementation details of these task for best/ efficient work.
Is the contact list is fetched everytime from my device whenever I enter the activity(which is actually redundant, because we maybe doing same work over and over again,
But again we cannot rely on saved data in DB/SharedPref as contacts data is dynamic and bound to change,
so need your expert suggestion how to actually make this work in best possible way(I have already written method to fetch contacts- so whould I fetch the contacts afresh everytime or any other way around is there, what the popular chat apps implemet to show the contacts everytime?
Personally, I would implement the fetching of the contacts on a background thread (using coroutines) that is called periodically (perhaps each time they the activity is created, as you suggested, or maybe once when they open the app).
Because it's on a background thread, it would prevent them using the app (or any dropped frames while the activity is created). They might have thousands of contacts or be using a really slow device.
Presumably, you would want to store some other data associated with each contact: for example, if you opened up a chat with the user, you would want to associate all the messages with the user, so I would use a database such a realm rather than shared preferences.
So the entire process would probably look something like this:
Opening the app triggers a service that fetches all of the user's contacts on a background thread.
Service updates the database, perhaps removing contacts which don't exist and adding the new ones.
If you were handling this in a view model, your fragment or activity could observe the list of contacts and update the UI once it had changed.

Having problems applying android code architecture/design patterns

I want to achieve the following:
I receive a notification via FCM with a specific id in the data properties. I get the id both when my app is in foreground and in the background/closed...so far so good.
Now I want to do the following:
Make a volley request to e.g. /books/id to get the corresponding book informations.
Save those informations persistently in a Room database
Display them in a RecyclerView
Scenario 1: App with RecyclerView is in foreground:
As soon as I receive a new id via the FCM notification data, I want my RecyclerView to append another list item with the newly fetched book informations.
Scenario 2: App is in background - notification gets clicked:
When the notification gets clicked, the MainActivity with the RecyclerView gets loaded, showing the newly fetched book informations.
Scenario 3: App is in background - notification gets swiped away:
After some time the notification was swiped away the user re-opens or restarts the app. The MainActivity with the RecyclerView and the newly fetched book informations is shown.
My problem:
I don't quite know how to structure my code to accomplish this.
I tried making the volley request inside of FCM's onMessageReceived() but that resulted in an error and I don't know where and how I could do that otherwise. I tested the volley request in another project and it worked there.
Currently I try to learn how to work with the Room database.
I would really appreciate if someone can give me an insight of how to accomplish something like that. I really need to get this to work soon.
Thanks in advance! :)
EDIT 1:
I dont't get the error inside of the onMessageReceived anymore. I receive the correct response with the given book id.
Now I want to save these informations into the Room database and reflect the change in my RecyclerView. I think I'm going to use androids architecture components like here:
android architecture components example
I think that's exactly what I need. However, I still don't quite understand how to deal with it and structure my code accordingly.
Scenario 1: You may save it in DB, or just do broadcast and save it for instance in Activity then display a list.
Scenario 2: You should keep all display data in DB. After click on notification when RecyclerView is in background, you should handle the intent that pass through android.intent.category.LAUNCHER of intent-filter and put some info there to make a request and update the list.
Scenario 3: Finally, just do a request, save in DB, then display all in the list. This is a good approche, it takes extra time, but you always keep it updated.
Also read about efficient hot swap data with DiffUtils

Android SQLite adding contacts 'dynamically'

In my Android app, an Activity performs a query to get the contacts on the phone and stores them into a sqlite database. For now, this is performed only once during the first launch of the Activity because that operation takes about two seconds to execute (which is actually a lot of time)
However that workaround comes with an issue: if the phone user adds some contact my app will never be able to get them. Then, I was wondering if it was possible for my app to ´listen ´ for that event and add the contact in the database when it occurs.
Is such a thing possible?
You can do it by using ContentObserver.
You have to simply
Implement a subclass of ContentObserver
you have to register your ContentObserver to listen the change.
For more detail how to do it you can follow this article Use Android’s ContentObserver in Your Code to Listen to Data Changes

How to efficiently update and get data from a SQLite database on Android?

I am making an app which gets a time schedule from a website and display it in my app in a nice ListView. To make the schedule available when the user is offline I am trying to save it in a SQLite database first and get the data from there. The idea is that the database gets updated when the user starts the app, when the user presses a refresh button and at set times by a service running in the background.
My question is: how can I efficiently update and get data from the database without using the UI thread, thus not blocking user interaction.
I think I should use a system which puts tasks in a queue so that the database gets updated before the data gets displayed in my ListView. Other than this I don't have a single clue how to realise such a system on Android.
Ok so you need to implement three things:
1) Get data from website to db: for this you should use an AsyncTask
2) Make a ListView that will get data from the db and update automatically. For this you will need to use a Loader which will handle the loading of the data on the background.
3) To use the loader you will need a ContentProvider to provide you with the cursor needed and notify the ListView on changes.
For all three there are many nice tutorials online, for example see here

How to restore an Activity along with its ListFragment (with data loaded from server)

I am sure `save-restore' have been discussed a lot but I couldn't grasp it yet. I have following scenario
Launch app -->MainActivity-->onCreate() -->Loads MyListFragment
MyListFragment gets data from server and in onListItemClick I am launching another activity. When user comes back (by clicking Up in ActionBar) to MainActivity, everything gets recreated including MyListFragment. Also it connects again to server to get data again.
I want to avoid data reloading from server and see user the same screen with Fragment and data in it.
Appreciate any help.
thanks
You need to seperate the service call from the UI. There isn't a way to preserve any arbitrary number of items across the activity being destroyed and re-created.
Use a service to load the data from the server. The service should populate a local SQLite database.
Then have your listview populate the data from the SQLite database.

Categories

Resources