Android Variables in Background - java

I'd like to know how I can set a variable that will stay the same after I have closed the app. In this case I don't want to make it with a SharedPreference or a DataBase.
Thanks in advance.

If the app gets closed any variable will be long gone on the next start.
The only way to keep data is to use persistence of some sort, and the most commonly used one is SharedPreferences.
You can alternatively write to a file, send your data to a server (and load it again on the next start), or use a database.
You can also make use of a Service which you keep running in the background, that keeps your values. But you will have no guarantee about when / how the system might stop it, and they would be lost—again—like before.
If you want to keep some value, you need to persist it.

Variables endure for the life cycle of an app. When user closes an app and that app is not running a service in the background everything is deleted. In some occurences(but this has a slight chance) static variables from previous session can be read incidentally if app is restarted again, but this is not a correct behaviour.
There are 3 ways to keep your data.
Write to file: You can create files in txt, json or in any other format you wish, and read from these files on runtime to get values from previous session. I don't prefer writing to file to keep data very much. If you don't know how to use database and want mess with it, you can use this.
Shared Preferences: This is generally for saving settings file with name and value pairs.
Writing to a database: You write your data to database. SQLite and Realm databases are the most popular ones.

Related

So am building an app and it has this feature that i dont know exactly how to go about it

I hope this makes a bit sense, basically, I have this feature in my app for tracking calories which consists of having this page that only appears the first time you use the feature and it asks you to add personal details (so it can make the right calculations), after that you get faced with a simple page that tracks your nutrition with a button for the user to insert the meals he has eaten, this page has to save the inserted data (via firebase) and then restart from 0 each and every day.
my first problem is I don't know how I make the page that only appears one time to save personal data(to be more precise I don't know how to make only appears the first time). and the second problem is how do I make the app automatically sends the given data at the end of each day?
interface in normal state, interface when adding the meals
hopefully, this 2 images will help you get a better grasp of what am trying to explain
don't worry am not looking for someone to straight up solve it all for me, I just need some orientation about what type of things/functions I need to do to solve these 2 problems
While #Narendra_Nath's answer might work, please note that is not a bulletproof solution. Why? Because a SharedPreferences doesn't persist across app uninstalls. This means that your user can install and uninstall the app and see the page as much as they want. So if you indeed want a user to see a screen only once, then you should consider storing that data in a database. Please note that SQLite isn't also a solution because when a user uninstalls the app, everything that is stored locally is wiped out. So what's the solution?
The best way to solve this would be to store the data in the cloud, either in Cloud Firestore or in the Realtime Database. So you can set a boolean variable and always check against it.
If you however intend to implement Firebase Authentication, then another solution would be to display the screen when your users are authenticated for the first time. So even if they will try to sign in on another device, install and uninstall the app, they won't be able to see the screen again.
Regarding the second problem, you should consider using Cloud Function for Firebase. It's the most elegant solution. If you want to somehow schedule an operation, then you should consider using Cloud Scheduler, as explained in my answer in the following post:
Is it not possible to have a code in the background who will be called every 24h?
Make the page that only appears one-time -> store a value in the shared preferences "isInfoShownToUser -> false" then do a check when the app starts to check if this value is false or true. If it is false show the "take the info" page .. then turn the value to false in the shared preferences.
How do I make the app automatically send data -> Use a Workmanager implementation to send data to the server (Firebase) at a particular time ..
Or use a implementation like the first one which uploads the data to the server just once everyday

How I can create history layout in android studio such as recent

I am building an app include edit text and button. The user can put text in the edit text, then click button to transfer him to the browser.
So, I would like to create other layout as recent to store the text that the user entered on the edit text.
How I can do that? I need the logic or code that can help me!
Also, should I have create database to store the data?
Example:
enter image description here
You can store data in multiple ways, and you will need to understand what is right for your use case. You can store data in memory, by simply creating a List with your data type and adding to it every time user will click a button, but then it will not persist between sessions with the app.
If you want the data to persist, then you would need to use permanent storage, and there are a lot of options here:
You could use Shared Preferences
You could use File System and save the data to a file
You could use a database i.e SQLite, and store data there
You could use external server, and get the data through REST API.
Generally, there is a good overview of data storage in Android in the documentation which also have code examples.
Every option comes in multiple ways to accomplish it. There are built-in solutions, and multiple libraries to help you with this task, but first of all, you will need to understand what is the predicted usage of this data. I.E Should user have access to the data from another device? Should the data be available offline? Will data have complex structure? How the app can expand in the future? e.t.c.
Only by knowing this you can design how you will handle it.
If you need logic or code to create view, then you will need ListView, or RecyclerView, Adapter for handling the data, extra xml layout file for single item of your data.

Saving multiple bits of data to be later retrieved on demand?

Trying to make an app in android studio that works as a fitness helper/tracker. On a daily basis and multiple times a day, the user enters data regarding their diet/exercise. Prior to doing so, the user selects which day they're currently on, making the data they input specific to the chosen date. I want to give users the ability to retrieve any available data for a specific day on demand.
I don't quite think sharedPreferences would work, so I was thinking about just saving to internal storage as a text file. The problem is that 1) with all the different files that would be created for even a single day, I feel that it would get messy and 2) I'm not sure how how to retrieve the specific file I'd need, parse it, then output the parsed data graphically if I'm only using a single constructor to make each text file.
I haven't yet delved deeply into using a SQLite DB, it seemed somewhat excessive for this, so I'm hoping someone can either help me figure out a better way to solve the problem or enlighten me as to a more efficient method.
Thanks in advance!

Which persistence method?

I'm building an application that downloads a set of images from a website, extracts some features from them and then allows a user to compare an image she submits to the downloaded set, to see which one is the closest. At the moment the application downloads the images and extracts the features from them. Then the image and the feature get wrapped in an object and stored in a map, with the key as the name of the image, and the value as the aforementioned wrapped object.
Because this is stored in memory, each time I start the application it has to go through the quite expensive process of downloading and feature extraction. It would be much quicker if it could just load this info from disk, but I'm not sure on the best way to go about it - I've thought about these options:
RDMS: something like Postgres or SQLite
NoSQL: something like
Voldemort or Reddis
Serialisation: use built in java methods to write
objects to a file (could also be used in conjunction with a DB
though...)
I want it to be really light weight; I want to keep the application as small as possible and keep configuration down to a minimum. For this reason serialisation seems like the way to go, but I'd like a second (or more) opinion on that, because something about doing it that way just feels wrong. I can't quite put my finger on why I feel like that...
I should also say that users can add images to the set when the application is running, I'd like to save these images too.
I wouldn't recommend serialzation - just too many pitfalls.
If what you have is really just a map, then i think any of the key-value stores ( like redis) would be appropriate.
If you have more complex data, then you might want to consider a database (whether SQL or no-sql).

Saving Android Application State Using Static Constants

In my Android application I have used a Constants class to store app data like login user information using static variables. I'm able to use that data throughout the app. But, when I leave my app in background for a long time and start it later, it crashes. The error stack contains a NullPointerException on the variable that I have referenced from Constants class.
When your application is cleared from memory, all static variables (which are obviously stored in memory) are cleared as well. The Saving Data Training details many of the available solutions to store data so that it can be properly restored even if the application is killed. Simple key-value pairs can be stored in Shared Preferences.
From static or any variables you couldn't retrieve the last changes after the application is shutdown. For that you need to save the values into file system or any lightweight databases(for example SQLite). Whenever you start you application you need to load into your static fields from the file or database.

Categories

Resources