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);
Related
I have an application, that should run some loading (I/O operations) before switching to other activity, that will display loaded information. I would like to load information completly, before switching and this causes some design troubles. I might could have used AsyncTask to run stuff in the background, but from the design perspective I dont want the whole context of the Application be leaked in order to respond to the results. The concept of Service looks more likely to fit my needs.
So let me denote the situation I can't solve from design perspective. Application starts, which causes some Activity to be created and so on. This one creates a service, which does I/O operations in the background. Upon finishing it, Application changes the Activity to the specified one. The question is, how should I handle the situation, when service finishes it's job while the app is in background. As a programmer, I would like to make my app either reopen the first activity and insta change to the following one or simply start with the "following" one. Any ideas?
I would like to make my app either reopen the first activity and insta change to the following one
You could this. When processing in your service finishes save a value in SharedPreference like this
Context ctx = getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("LoadNewActivity", true); // or false
editor.commit();
Now do this in your launcher activity onCreate()
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
boolean loadNewActivity = prefs.getBoolean("LoadNewActivity", false);
if(loadNewActivity)
{
Intent intent = new Intent(this, MyNewActivity.class);
startActivity(intent);
}
else
{
// Do normal startup
}
How can I set an Activity that will be the Launch Activity just one time ( when the user sign up )?
e.g.
I have a Settings Activity and a NewsFeed Activity.
The first time I want to lanch the app with the Settings Activity and, after the user fill the options, he goes to the NewsFeed Activity.
The second time, after the settings filled, I want to launch the app with NewsFeed Activity.
I google it but could not find any detailed explanation or tutorial.
Its very simple.
You can use preferences to store if this first time then show Settings Activity else always show NewsFeed Activity.
Below is the sample code to use Preferences
SharedPreferences prefs = this.getSharedPreferences("MyAppPrefs", MODE_WORLD_0);
boolean settingsFilled = prefs.getBoolean("SettingsFilled", false);
if(!settingsFilled)
{
//SHOW SETTINGS ACTIVITY
SharedPreferences prefs = this.getSharedPreferences("MyAppPrefs", 0);
Editor editor = prefs.edit();
editor.putBoolean("SettingsFilled", true);
editor.commit();
}
Use the above code in your NewsFeed Activity.
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();
}
In the last activity of my app there is some numbers displayed in edit text's and an option to add more to these totals, the add more button takes you back to start of the app, where the user does the same thing again inputs some numbers and ends up back on last activity.
What I want to do is if they press the add more button is save the numbers that are in the edit text's so I can add them to the new set of numbers coming in.
What I thinking is save the numbers in on Pause, but don't know how and then add them to new numbers in on Resume, am I on the right track, what is the best way to do this.
In the "source" activity do this:
SharedPreferences settings = context.getSharedPreferences(
"yourfilename.bla", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("key", value);
editor.commit();
In the target activity do this:
SharedPreferences preferences = getSharedPreferences(
"yourfilename.bla", Context.MODE_PRIVATE);
String value= preferences.getString("key", "");
The second parameter in getString is the default value you want returned if "key" isn't found.
Also Check these links
Android Using SharedPreferences to save and retrieve values - Counter
http://android-er.blogspot.in/2011/01/use-getsharedpreferences-to-retrieve.html
You can use Intent to do so. For example if you want to transfer an int.
//first activity
Intent intent = new Intent(this, FirstActivity.class);
intent.putExtra("myInt", number);
//Second Activity
Intent intent = this.getIntent();
int number = intent.getExtra("myInt", -1);
Try implementing onSaveInsance and onRestoreInstance of the activity. This would be called before onPause and before onResume.
Sample is here.
Also, to add the data from a different activity which would return to this activity then you can use the startActivityForResult and send the data as intent from the new activity before finishing..
you'd better use SharedPreferences....
http://developer.android.com/guide/topics/data/data-storage.html#pref
You can use startActivityForResult and send your data via Intent.
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.