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

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!

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.

Best method to show fixed Listview Data - Arraylist / sqlite / any other

My app contains about 8 activities having different Listview's. As the data shown in each activity is constant (cannot be changed by user), what method should I use to save the listview items ?
Should I make a arraylist, sqlite db, or other method.
As the list may be long I want a easy structured method to add data on my PC then shown it on my app.
*Adding data is updating my app with latest list
If you want to add data later on and have it updated on all user phones, you should make an API with a database behind it, where you can send an HTTP request to retrieve data for each list separately.
This way, you can change list content however you want and it will be the same on all devices and you don't have to store it on the phone (maybe only cache it to reduce load on the API). The only bad side is that you need a server and a domain.
May I suggest some simple backends: Flask(Python) or NodeJS (Javascript).

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

What is good way to obtain data for a travel application

I have ran into a small issue with an Android App that I am currently working on.
I have a list of points of interest declared in an XML file. The POI's look like this:
<hotel>
<name>Hotel Atlas</name>
<latitude>45.612079</latitude>
<longitude>25.660756</longitude>
<thumb_url>/hotels/atlas.jpg</thumb_url>
<phone>039999999</phone>
</hotel>
The problem that I have is that there are about 200 points of interest and every time the users access the app the XML is read every time from an online location and that means high volumes of internet traffic. The XML is stored online so I can update it every time I want to add another POI and not force users to update the app. Is there any way to send this data to the app and not require to download the entire XML?
I have not yet found an optimum sollution for this and decided to ask for some opinions.
Thanks, Vlad
There are only 2 possible ways:
Either give static database with all POI or
Load data from web whenever required.
Now you can do one thing, keep static database with App. Make web call once app starts and check for the updated data if any changes you made in server database. If there is any change on server database, then update only those data to the local(static) database.
For implementing above step, you must have to pass Last Updated date to/from server. I mean whenever you make web call, you need to pass last updated date from the client, server will check for new data to be updated post this date. Server will store client date as updated date and return back to client.
Maybe it's possible to store POIs like "One POI - One file"? In this case you can easily read only new points from server

issue with appengine dynamic instance

I have a class which does something (initialization ) when my app first starts.This initialization code is present in static block .So normally should be executed only once when the class loads for first time.
Now when the problem is as my instance is dynamic when there are no requests coming to my server my app gets unloaded .So next time when a request comes app is loaded dynamically and initialization code runs again.This is what i dont want.
I know this problem can be solved by using resident instance.But i guess resident instance is not available in free quota.Correct me if i am wrong.
Is there any way by which i can get away with this problem?
Storing the result of calculation in db is one option buts its not feasible as i want that data quickly?
How about memchace?Will it work?Will the data in memchace preserverd across dynamic loading of my app?
Note:I am not explicitly unloading my instance but app engine does it when my app doesnot get any requests.
Memcache data can disappear at any time. Only datastore data is guaranteed to be persistent.
You can store your initialisation data and fetch it in the order:
static data members(?)
memcache data
datastore data
(this is what I do) if you don't want to take up Nick's suggestion.
Or you can schedule a task that periodically runs every 10 minutes, this instance will be kept loaded. (ensure to have threadsafe on)

Categories

Resources