Is there any way to delete or reset shared preferences data when re-installing an app, without uninstalling the app and installing it?
What I mean to say is, currently, I am developing an app which uses shared preferences, but as I am still developing it, I keep running and uploading the app to the test phone through Eclipse after I make changes. Currently I am unable to run the app from the very beginning of its expected process ( after the first time) , without uninstalling the older version and then uploading the app again.
For this in your launching activity onCreate() method check whether the shared preference file exist or not if it is exist delete it.and later create it where ever you want..
you can check the preference file exist or not like this..
public boolean isFirstTime() {
return getDatabasePath("your file name").exists();
}
Clear the activities like :
Intent intent = new Intent(this, Login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Clear ur shared Pref :
SharedPreferences pref = this.getSharedPreferences("mypref", Context.MODE_PRIVATE);
getSharedPreferences("pref", Context.MODE_PRIVATE).edit().clear().commit();
Check in your launch activity whether to clear the preferences or not with a function like this:
SharedPreferences prefs = this.getSharedPreferences("prefs", Context.MODE_PRIVATE);
if (!prefs.getBoolean("FirstRun", true)) {
// Not the first time so clear prefs
prefs.edit().clear().commit();
} else {
// Set the value for future runs
prefs.edit().putBoolean("FirstRun", false).commit();
}
Related
The scenario is if the user downloads the app for first time, I ask them basic questions (say Activity A) and request them to sign up (Activity B).
This is a one time process only after installing the app. After that whenever they open the app, I am planning to take them straight away in to the app (Activity C).
How should I do this? I am a newbie to Android programming. But I am not able to think about this scenario. I don't want to use database.
You need a persistent storage mechanism to save the state of the user (logged in or not). There are various ways you can do this. The easiest is SharedPreference which will store the user state locally. You can also store this information in your remote server and validate user each time she opens the app although this might be going a bit overboard in most cases.
Try using SharedPreferences. In ActivityA in on create check if SharedPreferences contain a certain value which decides if the user is signed up. If it not set or it does not have the required value, redirect the user to ActivityB or else ActivityC
Code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences pref = this.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
if(pref.contains("MY_KEY") && pref.getBoolean("MY_KEY", false)){ //first stratup or user has not signed in yet
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
} else { //already signed up
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
}
setContentView(R.layout.activity_main);
}
Dont forget to save/insert value inside SharedPreferences after the user sign up.
Code:
SharedPreferences pref = this.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
if(sign_up_success){
editor.putBoolean("MY_KEY", true);
editor.commit();
}
The basic flow should be as follows Splash Screen -> OnBoarding Screen(Signup) -> MainActivity On the Splash Screen you need to check for a prefs value lets call it isSignedUp which is false if the user has not signed up. Its pretty straightforward from here. Every time you launch your app you need to check if the preference if isSignedUp is true or false and based on the value show the screen accordingly. So if the value is true that means the user has signed up and show him the main screen else show him the sing up screen.
Use Android SharedPreferences, it can be used to store data which can be used to keep the answers in cache.
I am developing an android device anti-theft application in which the user's phone would automatically send a sms to the recovery mobile numbers with new numbers' details on boot. I used SmsManager with BroadCastReceiver and everything was good till that. But I want the UI part to be visible when I install the app to get the recovery numbers from the users only for the first time and then hide the app and only send sms'on boot later(after first time,work only on background). Is there a way to achieve this?
Use PackageManager to disable the Activity that has the main launch intent filter after setup is complete.
So many possibilities which you can try out are available
Use shared preferences in application. This checks whether our application installed is populated with the values that you want to cross check during boot up time
Use SQLITE DB to store values which can be encrypted. Check for the business logic and process accordingly.
Update data in your server and every time app is up , cross check with server data and run your logic.
File system (creating file and writing/reading data) This is the last and not advisable option
If you want references , how to make all these pls refer the following links
Android Shared Preference ExampleAndroid Sqlite exampleAndroid file System example
I guess you need to launch the Activity exactly once after it has been installed. In that case, when that specific activity is launched you can create a SharedPreference and save a value in there! And next time you launch, check if that value exists, if it doesn't launch the activity, else do the other option. Here is how I did it.
In the Activity to be launched once:
SharedPreferences pref = getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
SharedPreferences.Editor ed = pref.edit();
ed.putString("HASH", hash_received);
ed.putString("MOB",mob_no);
ed.apply();
And then in Activity before that, I implemented:
SharedPreferences pref = getSharedPreferences("ActivitySession", Context.MODE_PRIVATE);
String abc = pref.getString("HASH","");
if(abc.equals(Test))
{
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
else
{
startActivity(new Intent(Splash.this, FeedActivity.class));
finish();
}
Hope it helps! Happy coding!
I have the following situation:
One activity that is updated from a service. When the update is made, some preferences are written. Everything fine so far.
When the activity is not visible and the service decides to do an update, I get a notification. Clicking on this notification opens the activity where I expect to find the previously stored preferences, but nothing is there.
I saw that when clicking on the notification the onCreate() method of the activity is called and this is where I create an arrayadapter which contains the preferences, unfortunately the preferences are somehow not there.
Please keep in mind that:
I use commit() when I update the preferences
I access the SharedPreferences this way (when reading and when writing):
SharedPreferences sharedPref = context.getSharedPreferences(
SHARED_PREF_FILE, Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
where SHARED_PREF_FILE is a constant. While debugging I found out that the sharedPref object is pointing to the same xml file (/data/data/........xml), so this should be okay.
Best regards
Edit (some code):
I have a class (PrefManager) managing the SharedPreferences:
public static Set<String> retrieveData(Context context) {
SharedPreferences sharedPref = context.getSharedPreferences(
SHARED_PREF_FILE, Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
return sharedPref.getStringSet(MY_KEY,
new HashSet<String>());
}
In the Activity's onCreate() I have this:
myAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, PrefManager.retrieveData(this));
Answering my own question, in case someone stumbles upon this:
I was able to make it work by adding this to the intent that was used for the creation of the notification:
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
I am working on an application which contain register activity as it's first page which is on tabs. i want that once the user is register then whenever the user starts the application it should always run from main menu screen and should never display the register screen till the user uninstall the application and reinstall it again.
you can use SharedPreferences. This is an example:
SharedPreferences mPrefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
So you can check if firstTime is true doing this:
SharedPreferences mPrefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
if(mPrefs.getBoolean(firstTime, false){
//show screen
}
Set your AndroidManifest to start the MainActivity as the default activity.
Then in the onCreate check if the user has registered (perhaps store this in SharedPreferences), if they have NOT registered - instantly start the intent for your RegisterActivity, otherwise carry on as normal.
I am writing a android application where I want to register my application to remoter server when application is first launched on installation. Application will register to remoter server itself without taking any user input. How Can I track whether this is a first application launch after installation ?
By setting a shared preference:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = prefs.edit();
edit.setBoolean("launched", true);
edit.commit();
You can check for it on each launch with:
boolean launched = prefs.getBoolean("launched", false);