I have Settings retrieval issue in SettingsActivity.it is always giving me the last saved value.
Though MainActivity giving me the correct value as soon as Settings Activity is closed.
For Example:-
1. Main Activity is launched first
2. Clicked Settings menu button
3. SettingsActivity is launched
4. Select preference "Set username"
5. Opens dialog and fill name say "test", hit ok
6. while retrieving "Set username" value in SettingsActivity, it gives the me the last value(blank if nothing was there or last valid value)
7. Exit settings, MainActivity is launched. Now valid value of "Set username" is appeared as "test"
ISSUE:- Why SettingsActivity is not giving me lastest value of "Set username" as "test"
I am very new to this, Please help ....
Following are my source/xml files
MainActivity.java
[HIGH]
package com.example.settingsexample;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int RESULT_SETTINGS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView settingsTextView1 = (TextView) findViewById(R.id.textUserSettings1);
settingsTextView1.setText(" Settings:- ");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.menu_settings:
Intent i = new Intent(this, UserSettingActivity.class);
startActivityForResult(i, RESULT_SETTINGS);
return true;
default:
Toast.makeText(getApplicationContext(), "nothing", Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SETTINGS:
showUserSettings();
break;
}
}
private void showUserSettings() {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this);
StringBuilder builder = new StringBuilder();
builder.append("Username: "
+ sharedPrefs.getString("prefUsername", "NULL"));
Toast.makeText(getApplicationContext(), builder.toString(), Toast.LENGTH_SHORT).show();
TextView settingsTextView = (TextView) findViewById(R.id.textUserSettings);
settingsTextView.setText(builder.toString());
}
}
[/HIGH]
SettingActivity.java
[HIGH]
package com.example.settingsexample;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.widget.Toast;
public class SettingActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_settings);
final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
//----------------------------user name-----------------------------------------
this.findPreference("prefUsername").setOnPreferenceChangeListener(
new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,Object newValue)
{
Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_SHORT).show();
SharedPreferences.Editor editor = sharedPrefs.edit();
String Username = sharedPrefs.getString("prefUsername", "NULL");
Toast.makeText(getApplicationContext(), "Value before:- "+Username, Toast.LENGTH_SHORT).show();
editor.putString("prefUsername",Username);
editor.commit();
Username = sharedPrefs.getString("prefUsername", "NULL");
Toast.makeText(getApplicationContext(), "Value after:- "+Username, Toast.LENGTH_SHORT).show();
return true;
}
});
//---------------------------- user name-----------------------------------------
}
}
[/HIGH]
preference_settings.xml
[HIGH]
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="@string/pref_user_profile"
android:key="pref_key_storage_settings" >
<EditTextPreference
android:title="@string/pref_user_name"
android:summary="@string/pref_user_name_summary"
android:key="prefUsername"/>
</PreferenceCategory>
</PreferenceScreen>
[/HIGH]
activity_main.xml
[HIGH]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textUserSettings1"
android:layout_width="101dp"
android:layout_height="75dp"
tools:context=".MainActivity" />
<TextView
android:id="@+id/textUserSettings"
android:layout_width="wrap_content"
android:layout_height="81dp"
android:layout_weight="1"
tools:context=".MainActivity" />
</LinearLayout>
[/HIGH]
Though MainActivity giving me the correct value as soon as Settings Activity is closed.
For Example:-
1. Main Activity is launched first
2. Clicked Settings menu button
3. SettingsActivity is launched
4. Select preference "Set username"
5. Opens dialog and fill name say "test", hit ok
6. while retrieving "Set username" value in SettingsActivity, it gives the me the last value(blank if nothing was there or last valid value)
7. Exit settings, MainActivity is launched. Now valid value of "Set username" is appeared as "test"
ISSUE:- Why SettingsActivity is not giving me lastest value of "Set username" as "test"
I am very new to this, Please help ....
Following are my source/xml files
MainActivity.java
[HIGH]
package com.example.settingsexample;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int RESULT_SETTINGS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView settingsTextView1 = (TextView) findViewById(R.id.textUserSettings1);
settingsTextView1.setText(" Settings:- ");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.menu_settings:
Intent i = new Intent(this, UserSettingActivity.class);
startActivityForResult(i, RESULT_SETTINGS);
return true;
default:
Toast.makeText(getApplicationContext(), "nothing", Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SETTINGS:
showUserSettings();
break;
}
}
private void showUserSettings() {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this);
StringBuilder builder = new StringBuilder();
builder.append("Username: "
+ sharedPrefs.getString("prefUsername", "NULL"));
Toast.makeText(getApplicationContext(), builder.toString(), Toast.LENGTH_SHORT).show();
TextView settingsTextView = (TextView) findViewById(R.id.textUserSettings);
settingsTextView.setText(builder.toString());
}
}
[/HIGH]
SettingActivity.java
[HIGH]
package com.example.settingsexample;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.widget.Toast;
public class SettingActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_settings);
final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
//----------------------------user name-----------------------------------------
this.findPreference("prefUsername").setOnPreferenceChangeListener(
new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,Object newValue)
{
Toast.makeText(getApplicationContext(), "Changed", Toast.LENGTH_SHORT).show();
SharedPreferences.Editor editor = sharedPrefs.edit();
String Username = sharedPrefs.getString("prefUsername", "NULL");
Toast.makeText(getApplicationContext(), "Value before:- "+Username, Toast.LENGTH_SHORT).show();
editor.putString("prefUsername",Username);
editor.commit();
Username = sharedPrefs.getString("prefUsername", "NULL");
Toast.makeText(getApplicationContext(), "Value after:- "+Username, Toast.LENGTH_SHORT).show();
return true;
}
});
//---------------------------- user name-----------------------------------------
}
}
[/HIGH]
preference_settings.xml
[HIGH]
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="@string/pref_user_profile"
android:key="pref_key_storage_settings" >
<EditTextPreference
android:title="@string/pref_user_name"
android:summary="@string/pref_user_name_summary"
android:key="prefUsername"/>
</PreferenceCategory>
</PreferenceScreen>
[/HIGH]
activity_main.xml
[HIGH]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textUserSettings1"
android:layout_width="101dp"
android:layout_height="75dp"
tools:context=".MainActivity" />
<TextView
android:id="@+id/textUserSettings"
android:layout_width="wrap_content"
android:layout_height="81dp"
android:layout_weight="1"
tools:context=".MainActivity" />
</LinearLayout>
[/HIGH]