Confusion on Activity Lifecycle - android

I have one app which authenticate user to access internal activity, App is having several activity which gets data from previous activity
So I am bit confused on activity life cycle
I read Bundle data into local variable in activity OnCreate and reading DB in async task based on that data. this DB data is displayed on my app.
Now if I switch to another app, will my app have that local variable/Static variable data (read from DB data) with them,
I have SingleInstance class which keeps user ticket etc informations, Is that variable keeps in memory of app if we got phone call in between.
I am bit confused on this part.
As document suggest that you need to store unsaved data in shared preferences.
It would be good if someone highlight on that..

Well, first of all you need to make sure you understand the difference between a new app and a new activity according to the sentence:
Now if I switch to another app, will my app have that DB data with them after switching back to my app
If you actually mean different apps, the answer is "NO in another app(in case is your app too)" you cannot have access to another application DB (at least not directly), the only way is if that application implemented a content provider to share its data with another application, by default android applications are like sandbox and do not share any information with other apps unless specifically declared, there's another rules between two apps signed with same key but that's a different story and a huge advance topic not related to this question...
In case that you " mean go to another app (not mine) and then try to use it from MY app again " the answer is "YES", the information in the database is accessible among all the building components like "activity, service" that belong to the same application, information in database is persisted even after closing completely the app (and so is shared preference), but there's a huge different between shared preference and DB, and knowing when to use one or another differentiates between good and excellent developers, golden rule (but not the only or the best) is that, DB is used for complex queries and relational data that needs some sort of preprocessing or postprocessing to get the proper values, mean while shared preference is just a "key/value" map that persist in the application context
Hope this Helps.
Regards!

Related

What approaches should I use to share complex object across any class

On example we have an app that tracks Taxi drivers, a taxi driver log in into application and pick an order to drive for given customer.
In this case we have an Order class that represents a lot of data, it's very important to have access to this object from anywhere while still being able to modify it.
From now we can:
pass it by putting extra data to an intent, but it's not something
that makes code readable, in some cases it won't be a solution
deserialize the object into json and store it into cache, we still
need to constantly update this value which is a headache when more
complex objects are in game, one mistake and god knows when the
mismatch happens
store it into class that extends Application, this
is approach I am testing right now, but I am having doubts about this
as when app crashes / android will release app resources randomly, I am in need to restore given object state using
black magic with cache anyway
This is a problem that I believe is shared across many applications, like Spotify's current playing track, etc.
Are there other approaches?
Try using a sqlite database. In this case, you can access your data anywhere from your app if you create a robust GetOrders API method yourself.
Here is a tutorial of creating a database.
In this case, you will never lose any data if app exits or crashes. Also, it really makes no difference compared with caching your data in memory.

Import DataBase from another app

I am learning content provider. Now I can get the data base values from one app to another app using of Content provider. Now I want to get entire database from one app to another app.
Now my try is get the value form A app and store that in B app. But the problem is A app having more than 100 tables. So that I decided to get the entire database from A app to B app. Is it possible to get the database from A app and import to B app?
Thanks in Advance.
On an un-rooted device? No (sandbox). From the application level? No (sandbox again). From adb on an rooted device? Yes.
Any usecase that might need to utilize such functionality? I highly doubt it. Well, maybe if one lost his signing key and tries to rescue the live data from the old app within the new one. Or just trying to be evil by stealing data for blackmailing purposes.
But even if you manage the database, sometimes ContentProviders are more then just a layer around the database (see ContactProvider for example). So even if you have the tables they might be useless unless you also have the provider logic.

Android - Already Loaded data not display when first time start the application from recent app

In my application, I have some data to display in the dashboard which are coming from the sqlite db. But when I put the app to the recent app and open the app from the recent app list on next day, those data don't display and showing the error message (that I set to show when there is no data in the sqlite).
To give a better idea about my issue, those are the basic steps.
Install application
Open Application
Navigate through pages
Keep the device
Open Application on Next day
Inspect the behaviour of Dashboard(No data showing)
I think android may auto kill the apps in the recent app.isn't it?.
How can I solve this issue?.
If the data that you are storing are only with respect to your Activity and other classes, Android will clear off data as per priority for availing memory.
This is applicable to static variables also. Resolution for this will be storing the data using Parcelable interface.
Store data either using Parcelable or in Sqlite.
Retrieve the same when the Activity/Fragment is loaded again
Use plugin's available to generate these objects for ease of programming like android-parcelable-intellij-plugin-kotlin
For sample implementation use this reference StackOverflowLink
I don't know how the app flow have been designed since there is no source code, but the following lifecycle will help you figure out the point where you are losing your data (I am assuming you are talking about the unsaved data).
Based on this, you might have to implement the onPause() and onResume() accordingly.

How to persist Android purchases throughout activities

I already know how to persist normal data throughout Android activities, but I'm wondering if the same methods are safe to use for Android purchase data.
I'm making a free app that uses in-app purchases to unlock all the content. This is something that I'm currently checking in the splash screen activity in order to customize the UI, however I would also need this information throughout the app.
Is it best to query in each activity, or is a Shared Pref safe enough?
As a note, I'm using v3 of Android' billing library.
Thanks
Shared Preferences are OK for this. Your data is sandboxed the same as if it were a database. (Security-wise, both are just as safe)
You will need to query either the database, or SharedPreferences to get this data in your Activity regardless. Doesn't really matter from which data store you retrieve it.
You don't have to worry about users changing your data (somehow changing their local status to 'paid') as they aren't able to change your SharedPrefs (or database values) - since they don't have access to these secure data stores.

Which is the right place to store data for a wizard-like application?

my application consists of < 10 activities, which cover the wizard part. Each of these activities should collect user data and save the current status, which should be resumed, if the application is unexpectedly terminated.
Now, as the collected data seems to be 'private', the Dev Guide suggests three possibilities to save data:
Shared PreferencesSeems appropriate for my purpose; key/value would be perfectly fine for my kind of data
Internal StorageImo an I/O on every intent is an overkill
SQLite DatabaseFine, too
The thing I'm curious about now, is this the right approach to choose a home for my data? Are there any guidelines or best practices concerning storage on Android devices?
Shared Preferences uses IO - as it just an xml file in data folder of your application. Choose the one which simplier for you to implement. I would use Shared Preferences.
As for "home for data" - it is ok. The storage possibilities are already well-designed for you, so you shouldn't be worry about it. Just use the one which fits your purposes the best.

Categories

Resources