my Sharedpreference does not save when I restart an application
I only have 2 activities which are Main and second activities however sharedpreference works between 2 activities but when I restarted it it never goes to the if statement(Main activity) if(prefs != null) which means prefs is always null when I restart it. Does sharedpreference actually saves data when the application is closed? Thx in advance :)
MainActivity
package com.example.hongsukchoi.rentaltruck;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.content.SharedPreferences;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private EditText mile_edittext;
private RadioGroup radio;
private Button submit_button;
double truck_price;
double mile_price;
double total_price;
private int truck_type;
private RadioButton ten;
private RadioButton seventeen;
private RadioButton twentysix;
SharedPreferences prefs;
SharedPreferences.Editor editor;
Set<String> mySet = new HashSet<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
ten = (RadioButton) findViewById(R.id.tenfeettruck);
seventeen = (RadioButton) findViewById(R.id.seventeenfeettruck);
twentysix = (RadioButton) findViewById(R.id.twentysixfeettruck);
//ten.setChecked(true);
if(prefs != null) {
System.out.println("This is what I am looking for " + prefs.getAll());
//R.id.checked
ten.setChecked(true);
}
System.out.println("Application is now started");
//ArrayList<String> allPrefs = new ArrayList(prefs.getStringSet("total_set", null));
// System.out.println("HERE ARE ALL THE PREFERENCES(Main acivity): " + allPrefs.toString());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("type", MODE_PRIVATE);
editor = prefs.edit();
radio = (RadioGroup) findViewById(R.id.truckRadio);
submit_button = (Button) findViewById(R.id.submit_button);
mile_edittext = (EditText) findViewById(R.id.mile_edittext);
submit_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String temp3 = mile_edittext.getText().toString();
mile_price = Double.parseDouble(temp3);
total_price = truck_price + (mile_price*2);
String total2 = String.valueOf(total_price);
//editor.putString("total", total2);
//editor.putStringSet("total_set", mySet);
editor.putInt("type", truck_type);
editor.commit();
Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtra("total",total2);
//i.putExtra("type", truck_type);
startActivity(i);
}
});
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.tenfeettruck:
if (checked)
truck_price = 19.99;
truck_type = 10;
mySet.add(String.valueOf(truck_type));
break;
case R.id.seventeenfeettruck:
if (checked)
truck_price = 29.99;
truck_type = 17;
mySet.add(String.valueOf(truck_type));
break;
case R.id.twentysixfeettruck:
if (checked)
truck_price = 39.99;
truck_type = 26;
mySet.add(String.valueOf(truck_type));
break;
}
}
}
SecondActivity
package com.example.hongsukchoi.rentaltruck;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class SecondActivity extends Activity {
private double total_price;
private ImageView myImgView;
private TextView Total_displaying;
private TextView whichtruck;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
myImgView = (ImageView)findViewById(R.id.truckimage);
whichtruck = (TextView)findViewById(R.id.whichtruck);
SharedPreferences prefs = getSharedPreferences("type", MODE_PRIVATE);
SharedPreferences prefs_set = getSharedPreferences("total_set", MODE_PRIVATE);
if(prefs.getStringSet("total_set", null) != null) {
ArrayList<String> allPrefs = new ArrayList(prefs.getStringSet("total_set", null));
System.out.println("HERE ARE ALL THE PREFERENCES(Second acivity): " + allPrefs.toString());
}
//SharedPreferences type = getSharedPreferences("type", MODE_PRIVATE);
Intent i = getIntent();
String temp = i.getExtras().getString("total");
int type_temp = prefs.getInt("type", 0);
if(temp != null) {
total_price = Double.parseDouble(temp);
}
if(type_temp == 10)
{
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.truckten));
whichtruck.setText("is 10 feet truck!");
}
else if(type_temp == 17)
{
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.truckseventeen));
whichtruck.setText("is 17 feet truck!");
}
else if(type_temp == 26)
{
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.trucktwentysix));
whichtruck.setText("is 26 feet truck!");
}
Total_displaying = (TextView) findViewById(R.id.Total_displaying);
String display = Double.toString(total_price);
Total_displaying.setText(display);
}
}
add a comment |
I only have 2 activities which are Main and second activities however sharedpreference works between 2 activities but when I restarted it it never goes to the if statement(Main activity) if(prefs != null) which means prefs is always null when I restart it. Does sharedpreference actually saves data when the application is closed? Thx in advance :)
MainActivity
package com.example.hongsukchoi.rentaltruck;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.content.SharedPreferences;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private EditText mile_edittext;
private RadioGroup radio;
private Button submit_button;
double truck_price;
double mile_price;
double total_price;
private int truck_type;
private RadioButton ten;
private RadioButton seventeen;
private RadioButton twentysix;
SharedPreferences prefs;
SharedPreferences.Editor editor;
Set<String> mySet = new HashSet<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
ten = (RadioButton) findViewById(R.id.tenfeettruck);
seventeen = (RadioButton) findViewById(R.id.seventeenfeettruck);
twentysix = (RadioButton) findViewById(R.id.twentysixfeettruck);
//ten.setChecked(true);
if(prefs != null) {
System.out.println("This is what I am looking for " + prefs.getAll());
//R.id.checked
ten.setChecked(true);
}
System.out.println("Application is now started");
//ArrayList<String> allPrefs = new ArrayList(prefs.getStringSet("total_set", null));
// System.out.println("HERE ARE ALL THE PREFERENCES(Main acivity): " + allPrefs.toString());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("type", MODE_PRIVATE);
editor = prefs.edit();
radio = (RadioGroup) findViewById(R.id.truckRadio);
submit_button = (Button) findViewById(R.id.submit_button);
mile_edittext = (EditText) findViewById(R.id.mile_edittext);
submit_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String temp3 = mile_edittext.getText().toString();
mile_price = Double.parseDouble(temp3);
total_price = truck_price + (mile_price*2);
String total2 = String.valueOf(total_price);
//editor.putString("total", total2);
//editor.putStringSet("total_set", mySet);
editor.putInt("type", truck_type);
editor.commit();
Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtra("total",total2);
//i.putExtra("type", truck_type);
startActivity(i);
}
});
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.tenfeettruck:
if (checked)
truck_price = 19.99;
truck_type = 10;
mySet.add(String.valueOf(truck_type));
break;
case R.id.seventeenfeettruck:
if (checked)
truck_price = 29.99;
truck_type = 17;
mySet.add(String.valueOf(truck_type));
break;
case R.id.twentysixfeettruck:
if (checked)
truck_price = 39.99;
truck_type = 26;
mySet.add(String.valueOf(truck_type));
break;
}
}
}
SecondActivity
package com.example.hongsukchoi.rentaltruck;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class SecondActivity extends Activity {
private double total_price;
private ImageView myImgView;
private TextView Total_displaying;
private TextView whichtruck;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
myImgView = (ImageView)findViewById(R.id.truckimage);
whichtruck = (TextView)findViewById(R.id.whichtruck);
SharedPreferences prefs = getSharedPreferences("type", MODE_PRIVATE);
SharedPreferences prefs_set = getSharedPreferences("total_set", MODE_PRIVATE);
if(prefs.getStringSet("total_set", null) != null) {
ArrayList<String> allPrefs = new ArrayList(prefs.getStringSet("total_set", null));
System.out.println("HERE ARE ALL THE PREFERENCES(Second acivity): " + allPrefs.toString());
}
//SharedPreferences type = getSharedPreferences("type", MODE_PRIVATE);
Intent i = getIntent();
String temp = i.getExtras().getString("total");
int type_temp = prefs.getInt("type", 0);
if(temp != null) {
total_price = Double.parseDouble(temp);
}
if(type_temp == 10)
{
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.truckten));
whichtruck.setText("is 10 feet truck!");
}
else if(type_temp == 17)
{
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.truckseventeen));
whichtruck.setText("is 17 feet truck!");
}
else if(type_temp == 26)
{
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.trucktwentysix));
whichtruck.setText("is 26 feet truck!");
}
Total_displaying = (TextView) findViewById(R.id.Total_displaying);
String display = Double.toString(total_price);
Total_displaying.setText(display);
}
}
1
You have to initialise the prefs and editor variable in oncreate method.
– Lekr0
Nov 23 '18 at 18:47
add a comment |
I only have 2 activities which are Main and second activities however sharedpreference works between 2 activities but when I restarted it it never goes to the if statement(Main activity) if(prefs != null) which means prefs is always null when I restart it. Does sharedpreference actually saves data when the application is closed? Thx in advance :)
MainActivity
package com.example.hongsukchoi.rentaltruck;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.content.SharedPreferences;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private EditText mile_edittext;
private RadioGroup radio;
private Button submit_button;
double truck_price;
double mile_price;
double total_price;
private int truck_type;
private RadioButton ten;
private RadioButton seventeen;
private RadioButton twentysix;
SharedPreferences prefs;
SharedPreferences.Editor editor;
Set<String> mySet = new HashSet<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
ten = (RadioButton) findViewById(R.id.tenfeettruck);
seventeen = (RadioButton) findViewById(R.id.seventeenfeettruck);
twentysix = (RadioButton) findViewById(R.id.twentysixfeettruck);
//ten.setChecked(true);
if(prefs != null) {
System.out.println("This is what I am looking for " + prefs.getAll());
//R.id.checked
ten.setChecked(true);
}
System.out.println("Application is now started");
//ArrayList<String> allPrefs = new ArrayList(prefs.getStringSet("total_set", null));
// System.out.println("HERE ARE ALL THE PREFERENCES(Main acivity): " + allPrefs.toString());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("type", MODE_PRIVATE);
editor = prefs.edit();
radio = (RadioGroup) findViewById(R.id.truckRadio);
submit_button = (Button) findViewById(R.id.submit_button);
mile_edittext = (EditText) findViewById(R.id.mile_edittext);
submit_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String temp3 = mile_edittext.getText().toString();
mile_price = Double.parseDouble(temp3);
total_price = truck_price + (mile_price*2);
String total2 = String.valueOf(total_price);
//editor.putString("total", total2);
//editor.putStringSet("total_set", mySet);
editor.putInt("type", truck_type);
editor.commit();
Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtra("total",total2);
//i.putExtra("type", truck_type);
startActivity(i);
}
});
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.tenfeettruck:
if (checked)
truck_price = 19.99;
truck_type = 10;
mySet.add(String.valueOf(truck_type));
break;
case R.id.seventeenfeettruck:
if (checked)
truck_price = 29.99;
truck_type = 17;
mySet.add(String.valueOf(truck_type));
break;
case R.id.twentysixfeettruck:
if (checked)
truck_price = 39.99;
truck_type = 26;
mySet.add(String.valueOf(truck_type));
break;
}
}
}
SecondActivity
package com.example.hongsukchoi.rentaltruck;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class SecondActivity extends Activity {
private double total_price;
private ImageView myImgView;
private TextView Total_displaying;
private TextView whichtruck;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
myImgView = (ImageView)findViewById(R.id.truckimage);
whichtruck = (TextView)findViewById(R.id.whichtruck);
SharedPreferences prefs = getSharedPreferences("type", MODE_PRIVATE);
SharedPreferences prefs_set = getSharedPreferences("total_set", MODE_PRIVATE);
if(prefs.getStringSet("total_set", null) != null) {
ArrayList<String> allPrefs = new ArrayList(prefs.getStringSet("total_set", null));
System.out.println("HERE ARE ALL THE PREFERENCES(Second acivity): " + allPrefs.toString());
}
//SharedPreferences type = getSharedPreferences("type", MODE_PRIVATE);
Intent i = getIntent();
String temp = i.getExtras().getString("total");
int type_temp = prefs.getInt("type", 0);
if(temp != null) {
total_price = Double.parseDouble(temp);
}
if(type_temp == 10)
{
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.truckten));
whichtruck.setText("is 10 feet truck!");
}
else if(type_temp == 17)
{
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.truckseventeen));
whichtruck.setText("is 17 feet truck!");
}
else if(type_temp == 26)
{
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.trucktwentysix));
whichtruck.setText("is 26 feet truck!");
}
Total_displaying = (TextView) findViewById(R.id.Total_displaying);
String display = Double.toString(total_price);
Total_displaying.setText(display);
}
}
I only have 2 activities which are Main and second activities however sharedpreference works between 2 activities but when I restarted it it never goes to the if statement(Main activity) if(prefs != null) which means prefs is always null when I restart it. Does sharedpreference actually saves data when the application is closed? Thx in advance :)
MainActivity
package com.example.hongsukchoi.rentaltruck;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.content.SharedPreferences;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
private EditText mile_edittext;
private RadioGroup radio;
private Button submit_button;
double truck_price;
double mile_price;
double total_price;
private int truck_type;
private RadioButton ten;
private RadioButton seventeen;
private RadioButton twentysix;
SharedPreferences prefs;
SharedPreferences.Editor editor;
Set<String> mySet = new HashSet<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
ten = (RadioButton) findViewById(R.id.tenfeettruck);
seventeen = (RadioButton) findViewById(R.id.seventeenfeettruck);
twentysix = (RadioButton) findViewById(R.id.twentysixfeettruck);
//ten.setChecked(true);
if(prefs != null) {
System.out.println("This is what I am looking for " + prefs.getAll());
//R.id.checked
ten.setChecked(true);
}
System.out.println("Application is now started");
//ArrayList<String> allPrefs = new ArrayList(prefs.getStringSet("total_set", null));
// System.out.println("HERE ARE ALL THE PREFERENCES(Main acivity): " + allPrefs.toString());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = getSharedPreferences("type", MODE_PRIVATE);
editor = prefs.edit();
radio = (RadioGroup) findViewById(R.id.truckRadio);
submit_button = (Button) findViewById(R.id.submit_button);
mile_edittext = (EditText) findViewById(R.id.mile_edittext);
submit_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String temp3 = mile_edittext.getText().toString();
mile_price = Double.parseDouble(temp3);
total_price = truck_price + (mile_price*2);
String total2 = String.valueOf(total_price);
//editor.putString("total", total2);
//editor.putStringSet("total_set", mySet);
editor.putInt("type", truck_type);
editor.commit();
Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtra("total",total2);
//i.putExtra("type", truck_type);
startActivity(i);
}
});
}
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.tenfeettruck:
if (checked)
truck_price = 19.99;
truck_type = 10;
mySet.add(String.valueOf(truck_type));
break;
case R.id.seventeenfeettruck:
if (checked)
truck_price = 29.99;
truck_type = 17;
mySet.add(String.valueOf(truck_type));
break;
case R.id.twentysixfeettruck:
if (checked)
truck_price = 39.99;
truck_type = 26;
mySet.add(String.valueOf(truck_type));
break;
}
}
}
SecondActivity
package com.example.hongsukchoi.rentaltruck;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class SecondActivity extends Activity {
private double total_price;
private ImageView myImgView;
private TextView Total_displaying;
private TextView whichtruck;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
myImgView = (ImageView)findViewById(R.id.truckimage);
whichtruck = (TextView)findViewById(R.id.whichtruck);
SharedPreferences prefs = getSharedPreferences("type", MODE_PRIVATE);
SharedPreferences prefs_set = getSharedPreferences("total_set", MODE_PRIVATE);
if(prefs.getStringSet("total_set", null) != null) {
ArrayList<String> allPrefs = new ArrayList(prefs.getStringSet("total_set", null));
System.out.println("HERE ARE ALL THE PREFERENCES(Second acivity): " + allPrefs.toString());
}
//SharedPreferences type = getSharedPreferences("type", MODE_PRIVATE);
Intent i = getIntent();
String temp = i.getExtras().getString("total");
int type_temp = prefs.getInt("type", 0);
if(temp != null) {
total_price = Double.parseDouble(temp);
}
if(type_temp == 10)
{
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.truckten));
whichtruck.setText("is 10 feet truck!");
}
else if(type_temp == 17)
{
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.truckseventeen));
whichtruck.setText("is 17 feet truck!");
}
else if(type_temp == 26)
{
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.trucktwentysix));
whichtruck.setText("is 26 feet truck!");
}
Total_displaying = (TextView) findViewById(R.id.Total_displaying);
String display = Double.toString(total_price);
Total_displaying.setText(display);
}
}
edited Nov 23 '18 at 18:18
Hongsuk Choi
asked Nov 23 '18 at 18:06
Hongsuk ChoiHongsuk Choi
11
11
1
You have to initialise the prefs and editor variable in oncreate method.
– Lekr0
Nov 23 '18 at 18:47
add a comment |
1
You have to initialise the prefs and editor variable in oncreate method.
– Lekr0
Nov 23 '18 at 18:47
1
1
You have to initialise the prefs and editor variable in oncreate method.
– Lekr0
Nov 23 '18 at 18:47
You have to initialise the prefs and editor variable in oncreate method.
– Lekr0
Nov 23 '18 at 18:47
add a comment |
1 Answer
1
active
oldest
votes
sharedpreference do save data when the application is closed.
You are checking if "prefs" is not null before initiate it.
Try the code below and check if this solves your problem :
inside onCreate:
prefs = getSharedPreferences("type", MODE_PRIVATE);
inside onStart:
if(prefs != null) {
System.out.println("This is what I am looking for " + prefs.getAll());
//R.id.checked
ten.setChecked(true);
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451234%2fmy-sharedpreference-does-not-save-when-i-restart-an-application%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
sharedpreference do save data when the application is closed.
You are checking if "prefs" is not null before initiate it.
Try the code below and check if this solves your problem :
inside onCreate:
prefs = getSharedPreferences("type", MODE_PRIVATE);
inside onStart:
if(prefs != null) {
System.out.println("This is what I am looking for " + prefs.getAll());
//R.id.checked
ten.setChecked(true);
}
add a comment |
sharedpreference do save data when the application is closed.
You are checking if "prefs" is not null before initiate it.
Try the code below and check if this solves your problem :
inside onCreate:
prefs = getSharedPreferences("type", MODE_PRIVATE);
inside onStart:
if(prefs != null) {
System.out.println("This is what I am looking for " + prefs.getAll());
//R.id.checked
ten.setChecked(true);
}
add a comment |
sharedpreference do save data when the application is closed.
You are checking if "prefs" is not null before initiate it.
Try the code below and check if this solves your problem :
inside onCreate:
prefs = getSharedPreferences("type", MODE_PRIVATE);
inside onStart:
if(prefs != null) {
System.out.println("This is what I am looking for " + prefs.getAll());
//R.id.checked
ten.setChecked(true);
}
sharedpreference do save data when the application is closed.
You are checking if "prefs" is not null before initiate it.
Try the code below and check if this solves your problem :
inside onCreate:
prefs = getSharedPreferences("type", MODE_PRIVATE);
inside onStart:
if(prefs != null) {
System.out.println("This is what I am looking for " + prefs.getAll());
//R.id.checked
ten.setChecked(true);
}
edited Dec 20 '18 at 10:48
answered Nov 23 '18 at 18:14
Tamir AbutbulTamir Abutbul
928419
928419
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451234%2fmy-sharedpreference-does-not-save-when-i-restart-an-application%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You have to initialise the prefs and editor variable in oncreate method.
– Lekr0
Nov 23 '18 at 18:47