My app is mainly a timer that counts down from 20 hours.
I am wondering which method I should use for saving the information necessary for resuming the app with the timer showing the correct value.
I don't know if I should use savedInstanceState or SharedPreferences?
My plan of action is to:
1. create a TimeStamp on the "Timer Start" button
2. save the TimeStamp variable with either the savedInstanceState or SharedPreferences
3. When app is closed and re-opened, get the TimeStamp variable
4. change progressBar status to display the correct remaining time based on the TimeStamp instead of restarting the progressBar from 0.
You should go with shared preferences. Shared preferences are designed to hold persistent state while saved instance state is mainly designed for restarting an activity after configuration changes.
See Google Developer Activity JavaDoc page
Related
I want make a kind of a daily login reward.
I have a button named ClaimReward and A user can claim reward one time in a day!. if user claimed the reward then disable the button and if
24 hours passed then again enabled the button.
My try
I stored a today's date in firebase realtime database in millis (long) or timestamp - 1353534920
So, how can I do this in android
You can get this output with this logic.
Store the value you want to reset to the preset value using shared preferences
Store the date-time value from the system using the sharedPrefs.
On App startup check if the local time matches the time in sharedPrefereces.
If it matches -> do nothing
if it does not -> reset
PS: Here shared preferences is just an example you can use room/datastore or anything to perform this check on app startup.
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.
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
I am creating an alarm application. I currently have 2 activities, one called AddAlarm which creates a new alarm and allows users to enter data such as alarm time and title. The other activity is called Alarm and it displays all the alarms that are currently set in a ListView.
What I do not understand is:
1) How can I pass the data from the AddAlarm activity to a ListView item in the Alarm Activity?
2) How can I pass the data back to the AddAlarm activity when the user edits the alarm?
3) How can I store the data each alarm when the app restarts?
I have already tried to create a bundle for each alarm, however, I learned that these cannot be stored in SharedPreferences and I am not sure how bundles can be used in an arraylist.
Do you guys have any suggestions? Thanks a lot!
From your question, what is clearly understandable is that you are facing problems with data storage for your app.
You do not need to pass data from one Activity to the other, what you need to do is use a persistent storage mechanism, such as SQLite or Realm.
The idea is simple, I will list down some of the important points.
When the user adds new alarms from the AddAlarm activity, just store the data in the database
In the AlarmActivity with the ListView, make a query to find all the alarms which have not expired yet and show them
Whenever an alarm expires, make the necessary modifications to that particular alarm entry in the database so that it is not shown again
This would be an idea way to handle the scenario for your app. Please have a good read at this.
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.