Android and SQLite interactions: updating row with multiple intents - java

I have a program with a main activity which spawns 4 different intents with lists in each. There is a database used to track all of the choices from these lists. Currently with the way that startActivityForResult() works I am having the program send back a comma delimited string which is saved in an array in the main activity and upon main 'submit' it is all saved to the database. The lists represent choices made on a per-day basis but each list corresponds to the same day. I am using putExtra() to send the correct list to the activity.
This can be an issue if the user closes the program or android kills the app the array may be lost. I want to update the current dates data from each spawned intent but when I would do this it would create a new entry.
Main question: Can I set a global date value (that the user sets in main) that each intent can reference to use update for my sql statements. I want to make sure that there is no loss in data. Currently writing to the database is only done in the main activity upon submit but I would like to have the app write to the db from each intent.
This question is very abstract in my brain and if there is a need to clarify I will do my best. (I am at school and just brain storming things to try when I get home)

Write the array to Shared Preferences in onPause() and onTerminate(). You can use the array element number as the key and whatever is in that array element as the value.
I can't quite work out where you're going with the global date/time stamp, but if the point is to prevent data loss, Shared Prefs can be damned handy.

Related

How to persist data after application is terminated?

I want to persist some edit text data after application is terminated completely. I have two activities MainActivity1.java and MainActivity2.java.
If user enters data partially and closed the application when he comes back application need to be pick up all the data which he was entered in previous stage.
Scenario:
Hence I am using ListView I have map of editText values in the form of key value pair. Currently I am working on MainActivity2.java file which has list view. So I am saving the map data in to Internal Storage of device when MainActivity2 is on onPause() state. So on OnResume() it is showing all data into edit text.
Now the problem is if I terminate the application completely then it is not picking up the data which I have stored in my Internal Storage.
So if I need to store data when application is closed completely which method I need to call.
Note : I am using file read and write to store and get data from internal storage
Whenever you write any data into the Edittext, you may save it to your Shared Preference (if the data value is not much) else you can create a Database using SQLite or Room (if the set of data is higher).
In both the cases, the data will persist even if you close the application, make sure to save the data after you have input the data in the edit text.
https://developer.android.com/training/data-storage/shared-preferences -Shared Preference
https://developer.android.com/training/data-storage/sqlite - SQLite
Look SharedPreferences or SQLite. Once you do you will basically be saving your values in SharedPreferences or SQLite in an onPause() / onDestroy() method, and then in your onResume() / onCreate() methods you will retrieve the previously stored data.

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!

Java Android SharedPreferences issues

I'm making a game of Tetris on android as a project for school and right now im using shared preferences in order to save the current state of the game so that it can be resumed on a later time , i've come to realize that when you store over 100 or so preferences the sharedprefernces object starts working in a strange way , i can save everything but when i try to call the editor to clear (e.clear + e.commit) it wont remove the preferences.
i would appreciate any help regarding this issue
thanks
SharedPreferences are good and lightweight mechanism how to persist data.
But i think for game it's not a win at all. SharedPreferences are usually used for persisting non-structured data for example if you have some application that requires login and when User is logged in successfully you can save this state to SharedPreferences and in next Activities just check it whether User is logged in or not. But in the game you have (i guess for sure) structured data-structures (for instance players and their properties (values) like reached score, loses, wins etc.).
So i suggest you to think about another mechanism for data persisting. Specifically try to think about a possibility to use classic object serializing or and usage of SQLiteDatabase which provide more complex solution how to persist structured data.
A main advantage is that you can persist (serialize) whole objects and then simply deserialize them (and not persist them as specific "chunks" in SharedPreferences). Regarding to SQLite, it provides almost same solution as classic serializing but objects are represented as tables in database.
If you need to remove specific values use this:
SharedPreferences.Editor.remove() followed by commit()
To remove them all SharedPreferences.Editor.clear() followed by a commit()
(references here https://stackoverflow.com/a/3687333/1584654).
However If the values remain limitated, for Shared Preferences should not be an issue.

Categories

Resources