I have to store value for temporary use so I used SharedPrefences for that but in this case when the user goes to Setting -> Application -> Application and then clicks on Force stop and Clear data then SharedPrefences is lost. So is there any suggest to solve this problem?
You can use element android:manageSpaceActivity in your AndroidManifest.xml to point to an activity. So when the user goes to the details page of your app in Settings, there will be a button Manage space instead of Clear data. Clicking that button will bring your specified activity up. Then you can show the user options to manage data (database, preferences…).
The only work around to fix this problem is to use a web service and store your data on the server. You can make device IMEI as primary key to avoid duplication.
as already mentioned above, the only proper way to prevent the user from having the possibility to delete your app data is storing the data on the web and download when needed. You can do both and then perform a check on runtime. If the required data is missing, then it gets downloaded
Related
I've watched the Coursera android course and I have doubts how Android works in my application that I'm building. It does basic stuff, like register and show my info and do TODO using server as database. And I know it depends how I want to it begin built. Need some options.
Fragments can be built in ActionBar and whenever I want be called? Or is it bad to this way and stick with default menu built and hide it programmatically? Tried to google and didn't find any information.
The MainActivity is made to make http request POST to register name, email and password in my server. Is it possible to start the SecondActivity whenever he logged in? It's just like Instagram. But, I could set a condition to check depends if logged or not change the setContent(R.layout.main) or setContent(R.layout.second)?
When the register is done. All the data is saved on the server. But I want to check every time he changes activity or do some action if he's logged in or not. Could I use some thread with a flag(bool)? Could be a bad practice and reduce the performance? Or SharedPrefrences adding flag(bool)? But SharedPreferences are saved after the application is closed?
About to show my information in every Activity, like name, age and sex etc and such. Making request http just to show information is quite bad for my application, since it's just TODO app and I don't need to be connected to internet to see my info, but in case of edit, yes. So if I save in the SharedPreferences, but stays the same doubt, it will save after I close the application?
Since your question is likely to be drastically edited (as i advised in a comment) i quote the original text here.
Fragments can be built in ActionBar and whenever I want be called? Or
is it bad to this way and stick with default menu built and hide it
programmatically? Tried to google and didn't find any information.
Yes, fragments can be used whereever you like. But thats not really an answer.
Base your design on the various activities (think "what app screens should users navigate", and give these one fragment each.
The MainActivity is made to make http request POST to register name,
email and password in my server. Is it possible to start the
SecondActivity whenever he logged in? It's just like Instagram. But, I
could set a condition to check depends if logged or not change the
setContent(R.layout.main) or setContent(R.layout.second)?
Yes, you could have a LoginActivity that is called with startActivityForResult(). It would connect and return the success/failure. (See http://developer.android.com/training/basics/intents/result.html )
Also, keep in mind that HTTP POST is not encrypted, the password is sent as plain text.
When the register is done. All the data is saved on the server. But I
want to check every time he changes activity or do some action if he's
logged in or not. Could I use some thread with a flag(bool)? Could be
a bad practice and reduce the performance? Or SharedPrefrences adding
flag(bool)? But SharedPreferences are saved after the application is
closed?
Yes, SharedPreferences are stored after exiting the application. Ideal to simply implement app settings, but when storing lists of items you should look into other solutions. A JSON file may be of use, you could directly pull/push it from/to server.
About to show my information in every Activity, like name, age and sex
etc and such. Making request http is quite bad for my application,
since it's just TODO app and I don't need to be connected to internet
to see my info, but in case of edit, yes. So I saved in the
SharedPreferences, but stays the same doubt, it will save after I close the application?
See answer above. In short get a JSON string from the server, store it locally in a file. View and edit the local file, then upload it whenever you want. This way it works offline too, but can still download/upload the changes.
I understand how to create a one time activity that saves to preferences to make the activity appear only once. But can someone show me how to save the "first open" screen to sqlite. I want to prevent users from clearing the data and seeing the screen again.
to clarify
I am looking at a password login tutorial and i want the users to register a password with the app but once the password is set, I want the registration screen removed for security purposes. Can someone please help.
Thanks
Using SQLite as your storage is not going to help.
Android will store your database under /data/data/your.package.name/databases/yourfilename and thus, when a user clears the data for an app, it will remove any databases, along with shared preferences too.
I want to be able to prompt the users to enter the details required for the app only when it starts up for the first time (not other times). These details will be held in a database on the phone.
Would it be better to check the database each time or put these details in a shared preference? Furthermore, is this type of activity even possible on Android?
Depends on the details really. If it is a large amount of information you might consider using a Database. For just basic identification information, SharedPreferences should be fine.
If you do use a Database, you might consider loading them into your Application object. This would depend on how frequently you use this information in your app.
To check for the first start of the app, a SharedPreference is generally used AFAIK.
Use SharedPreferences or Database depending on the data complexity and its need later on..
Store a Flag in SharedPreferences and set this when user enters the data.
Check that shared preferences flag while on your first activity and redirects accordingly(to Details/one time page) if required.
here even if a user manually deletes data using Settings > Manage app > your app > clear data, Your app will know to get the details then from user....
I am having an android layout having to input some of the basic details of the user.
This layout screen or activity has to come only once after installation, but it should not come up after successive opening of the application.
Could you let me know how can this be achieved in android.
Looking forward to your reply.
thanks.
you can put a flag in shared preferences that you showed once the screen and it's no need to show it again. But when the user clears data, the flag will be deleted. As a hard way, you can save this flag on a server or cloud.
I think having a flag set in shared preference is the best way to do that. And i think if the user clears his cache , then he deserves to see the screen which is shown only once on installation. Similar is the behaviour which happens everywhere. Many websites store the user information in their cache and don't show user unnecessary information but if you clear the cache then you will see that information back again
How do I figure out, if an app is loaded for the first time after it has been downloaded. I mean when the app is first bought/downloaded from market, how will I figure it that this is the first time(never has been run on this phone) this is going to run hence I can throw up some registration activity?
yeah, I thought of declaring a table which is checked to see if it has any rows. If there were rows then this was because it had been loaded and we inserted a row.
But I want to avoid this, is their any API which can tell me that the user had used this app before on this phone?
Android has API to store preferences.
You can store a flag in SharedPreferences, read about shared preferences
You can have a flag that you track, i.e. isFirstTime. Have a check in your main activity:
if(isFirstTime)
// do registration
isFirstTime = false;
You can save the value in between executions as a shared preference. See Data Storage documentation for details on how to do that.
Write a property somewhere that persists on the first load and check for that on every load, if it is there then you know that it has already been loaded once. Or you might even just make it a load counter and maybe you could use that information as a statistic (report back some where)
every application has it's own directory for data (/data/data/).
just write an empty file there and check for it every time you start an application
I don't know of any Android Market API that will tell you if the user downloaded/used the app before.
I think that you'll have to do it yourself which gives you two choices to persist data across an app uninstall/reinstall cycle:
(1) save a file on the SD card an check that to see if the app was installed before or
(2) contact a server with some user specific info to check if it was downloaded/used before.
The problem with the SD card method is that its not effective if the user erases the file or switches SD cards.
For the server model, you'll need to have a server to record some piece of information like user id or device id or some mix.