Vimukthi Saranga Vimukthi Saranga - 16 days ago 5
Android Question

getSharedPrefernces return null only in one activity which works fine on another

I'm using following code to save the shared preference.

sharedpreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.putString("NIC",nic);
editor.putString("fName",fName);
editor.putString("mName",mName);
editor.putString("lName",lName);
editor.putString("aNo",aNo);
editor.putString("lane",aLane);
editor.putString("city",aCity);
editor.putString("contactNo",contactNo);
editor.putString("regDate",regDate);
editor.putString("cardNo",cardNo);
editor.putString("balance",balance);
editor.commit();


Using following code to get the shared preference in Profile activity and it's working fine.

private void getPrefs() {
SharedPreferences userDetails = Profile.this.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
id = userDetails.getString("NIC", "");
f_Name = userDetails.getString("fName", "");
m_Name = userDetails.getString("mName", "");
l_Name = userDetails.getString("lName", "");
contact_no = userDetails.getString("contactNo", "");
a_No = userDetails.getString("aNo", "");
a_Lane = userDetails.getString("lane", "");
a_City = userDetails.getString("city", "");
card_No = userDetails.getString("cardNo", "");
reg_Date = userDetails.getString("regDate", "");
}


But when I use the same in another activity, it returns null for the id.

private void getPrefs() {
SharedPreferences userDetails = Travel.this.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
id = userDetails.getString("NIC", "");
}


mainfest.xml

<activity
android:name=".Profile"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.pavsaranga.scat.PROFILE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Travel"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.pavsaranga.scat.TRAVEL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>


can anyone please help me to solve this issue?
the full code of the Travel Activity

public class Travel extends AppCompatActivity {

TableLayout tl;
StringBuilder sb;
String id, result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.travel);
tl = (TableLayout)findViewById(R.id.tlTravel);
getPrefs();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
getResponse();
}
});
t1.start();
}

private void getPrefs() {
SharedPreferences userDetails = getApplicationContext().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
id = userDetails.getString("NIC", "");
}}

Answer

change this or Profile.this to getApplicationContext()

Like :

     SharedPreferences userDetails = getApplicationContext()
.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);