Android SavedPrefrerences loading? - android

I am trying to make an app(http://pastebin.com/uWkP6XNY) that when you press a button, creates a custom sms message. The user can go to a second activity (http://pastebin.com/MK2NPV5R) thats full of edit-texts that when saved, will bring back strings to be used to change the custom sms.
The issues I am facing is how I initalize my variables with whats in savedpreferences. I put this in my onCreate method.
smsintroduction = (sp.getString("intro", "")); //these are both strings initalized at the top
smsbody = (sp.getString("body", ""));
On start up, since it can't get "intro" from the dictionary, it goes to a null string. I want to be able to use my save() function in my second activity to save, which I Think I already do, but be able to change my two strings above.
I put the code above to set the strings in a method that completes the finalized textbody, but it keeps giving me emptystrings.
The only thing that gets created is "!", as shown in finishedtext().

It looks like in your MainActivity in onCreateOptionsMenu you are overriding the sp member previously set in onCreate with
sp = getSharedPreferences("prefs", 0);
Try removing that line or setting those shared prefs to a different instance member.

Related

Modifying a Preference value in PreferenceActivity has no effect before Activity is closed

I am just trying to change the value of a Preference when the PreferenceActivity has been opened. As there is no "setValue" or similar on a Preference, I try
My code:
long value = System.currentTimeMillis()/1000;
PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putString("test",""+value).apply();
getPreferenceScreen().findPreference("test").setSummary(""+value);
My XML:
<EditTextPreference
android:key="test" />
What I expect:
When clicking on my Preference, it should display the value of time (same than summary) and let me edit it.
What happen:
The value is only changed after I closed the Activity. Next time I open the screen, the value is correct (but as in fact already changed to the next one)
First attempt:
Let s say value is 1521143527. Correctly written in the summary, but when I click on the Preference, the popup display an empty value.
Second attemp:
Summary has changed to 1521143540. When I click on Preference, I can edit the previous value (1521143527)
Third attempt:
New Summary, but Preference value is not changed and is still: 1521143540
etc...
Any idea what is wrong?
DIRTY WORKAROUND:
setPreferenceScreen(null);
addPreferencesFromResource(R.xml.preferences);
Will now force the preference to update, but that's really dirty, and I still don't understand...
If you look into the PreferenceFragment source code ,you can see that there is a method called bindPreferences() which binds the preference values to Views . Only in 2 scenarios this method is called,
When Activity created onActivityCreated(#Nullable Bundle savedInstanceState)
When addPreferencesFromResource() called. there is a handler which triggers the bindPreferences()
Other than this there is no way the views are updated. bindPreferences() is a private method , so you can't call this method outside of the class. So you should update your preferences before either those events.
You mentioned, As a workaround solution, you should update your preference first then call addPreferencesFromResource() . Like below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
long value = System.currentTimeMillis()/1000;
getPreferenceManager().getSharedPreferences().edit().putString("test",""+value).commit();
addPreferencesFromResource(R.xml.pref_general);
getPreferenceScreen().findPreference("test").setSummary(""+value);
}

Android : Get previous fragment name to do nothing in onCreate

I have a detected a bug in my app today : When a user clic on an item in my listview, it launch a new fragment with details about this item. But when the user is in this fragment, if he change orientation of the device, the onCreate method of my first class (which fetch my listview) is called, and the app crashed.
I would like to know if it's possible to get the name of the previous fragment, in order to add a test like the following in my onCreate method :
if (!fragmentName.equals("common")){
Log.d("INFO", "Do nothing in this case, because user was in detail fragment before !"),
} else {
super.onCreate(savedInstanceState);
refreshList(true);
}
I don't think it is possible to get the name of the previous fragment as its creating a new instance of your Fragment object.
If I am right in what you are trying to do you need to somehow store the object for which you are displaying data so that when the Fragment is recreated you can then retrieve the required object. One way is you keep the ID or a unique property of the object in SharedPreferences and then reacquire its data when it reloads from a singleton somewhere.
Basically you may need to change the way you retrieve your data or pass it from the original opening Fragment so that you can retrieve it again if the second Fragment is destroyed.

Android - Language change when clicking back

I have this settings section where I allow users to change the languages displayed within the app. When the user chooses a different language, the activity is reloaded so that the change of language can be applied. But the problem is, when the user clicks back right after changing the language, the language shown in the background activity is still the same.
So my question is, what should I do to apply the change of language when I get back to some activity on the background? I suppose I should do something to detect the change in onResume method, but I'm not sure what it is. If you have any suggestions, please let me know.
Thank you.
After several attempts, I have found the solution to my problem. On my onCreate method, I get the SharedPreferences that contains the value of current language, and get the current language:
SharedPrefrences languagepref = getSharedPreferences("language",MODE_PRIVATE);
String language = languagepref.getString("languageToLoad", Locale.getDefault().getDisplayLanguage());
Then, in my onResume method, I assign the value of the above mentioned variable language to a local variable, and update the value of language. Then I compare these two variables - if they are different, I will destroy the current activity and start another:
#Override
public void onResume(){
super.onResume();
String oldLanguage = language;
language = languagepref.getString("languageToLoad", Locale.getDefault().getDisplayLanguage());
if (!oldLanguage.equals(language)){
finish();
startActivity(getIntent());
}
}
And voila, that did the trick!
I would suggest using SharedPreferences. You can store a lang key with the associated value in there and update it when necessary. In your onResume() methods you can get the lang value and then populate views according the value stored.
SharedPreferences sharedPreferences;
sharedPreferences = this.getSharedPreferences("MyActivity", Activity.MODE_PRIVATE);
String lang = sharedPreferences.getString("lang", "en-GB");
SharedPreferences.Editor editor;
editor = sharedPreferences.edit();
editor.putString("lang", "en-US").commit();
That's the basics you need to get going.
Have you tried restarting the Activity after the change is done ?
You can Simply use
finish();
startActivity(getIntent());
to refresh an activity whenever it detects a preference change.
The built in back pressed function does not refresh the code,so do this after u you change the language.
#Override
public void onBackPressed()
{
//new MainActivity();
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
and in Main Activity class do this
public MainActivity() {
//1- This code is responsible for updating the change of all Strings from a language to another
//it will be called every time this activity is instantiated (da,since it is a constructor) , or when this activity gets
// called by an intent.
//2- Every String inside this Activity(ever fragment inside it ) will also be under the effect of change of language
LocalUtils.updateConfig(this);
}
Please review the following answer and add this to it. To get a full answer written by Roberto.B and LarsH:
Changing Locale within the app itself

How can I setText() (TextView) when the TextView is in another layout?

My app has 2 layouts (main layout) and (preference (prefs) layout).
When the MainActivity loads, I set setContentView(R.layout.main); - main layout
I need to then set text for a TextView in the preference layout, but it never gets set.
LayoutInflater factory = getLayoutInflater();
View inflate = factory.inflate(R.layout.prefs, null);
TextView eSerial = (TextView) inflate.findViewById(R.id.editTextSerial);
mSerial = "Test";
eSerial.setText(mSerial);
The way I get to the preference page is with a menu and then the page loads up with no change to TextView
I have searched and not found an answer yet.
Please help.
Thank you.
When the menu kicks off your prefs activity, you can populate the view with the values. user1853479 points out one way of doing this, which is to add the values to an intent. Assuming you want to store these prefs for future runs, you can also set any that for that specific run and save them in your local store. Another method is to create a singleton to store your settings, load it when your app starts, modify and save as needed, and access it from any of your activities.
Short answer: You can't do this.
Long answer:
If you are launching the preference page yourself, you must be creating an Intent to do so. Call putExtra() to store your text inside that intent. In your PreferenceActivity, call getIntent().getStringExtra() to get the text, then put it in your TextView.

Best way to backup user's inputs dealing with orientation changes

I am developing a simple apps that allow users to post articles on a website.
It's just a simple form with inputs for title et text and a send button.
To do this, I used a FragmentActivity with a Fragment called ArticleFragment inside.
I've read everywhere that the best way to save user's inputs was to override onPause() in the fragment, and to restore them in the onCreateView().
#Override
public void onPause() {
Log.i(TAG, "Pausing fragment");
Log.i(TAG, "Saving edits");
// Sauvegarde des saisies
EditText titreEdit = (EditText) getActivity().findViewById(R.id.titre_saisie);
EditText texteEdit = (EditText) getActivity().findViewById(R.id.texte_saisie);
String titre = titreEdit.getText().toString();
String texte = texteEdit.getText().toString();
SharedPreferences settings = getActivity().getPreferences(0);
Editor editor = settings.edit();
editor.putString("titre", titre);
editor.putString("texte", texte);
editor.commit();
Toast toast = Toast.makeText(getActivity(), R.string.article_enregistre, Toast.LENGTH_SHORT);
toast.show();
super.onPause();
}
As you can see, each time the activity is paused, a Toast is shown to inform the user that inputs have been saved.
That's what I wanted. But whenever the user changes the orientation of the screen, the popup is shown again (because Activity is paused, then destroyed) and this may annoye the user.
I tried to add android:configChanges="orientation" in the Manifest, but I think this is not the best way to deal with this issue.
I also tried to make a solution with onSaveInstance() but I do not understand how it works : sometimes onSavedInstance is null, sometimes not.
With all this, the main question is what is the best way to save user inputs, with orientation changes and back button, and where to restore what was saved.
Thank you!
Check if input values are empty in onBackPressed() and onClick of whatever button that takes the user to Home and display appropriate Toast message
Add android:configChanges="orientation"to manifest file.
Override onConfigurationChanged. Before you call super.onConfigurationChanged save the data.
If you have different layout files for portrait and landscape you have to remove all the views and setcontentview and initialize them(just like what you do on create ) after all these you can restore the data
I don't quite follow your logic. Lets see ... You have two edittext fields and save button, when user changes screen orientation on destroy you can save input in shared preferences or in Bundle and when onCreate() starts you can check if values are not empty and then show toast that there is a draft. If user hits back button you can also save input and display toast, if he/she hits send you just send input, right ?

Categories

Resources