I've had this issue for a few days now. It would seem that my shared preferences is not working for when I set my data. The user starts at main, which displays "oncreate" and then goes to the settings page(Where the data is automatically set(for now)), when they return to the main Activity, the data doesn't seem to want to come over. I am using the joda Time library and I'm trying to calculate the difference between two dates in time.
Main:(The onCreate Code and onResume code are the same)
public class MainActivity extends Activity {
TextView tv;
Button set;
//CONST
final static long MILLIS_IN_DAY = 86400000;
final static long MILLIS_IN_HOUR = 3600000;
final static long MILLIS_IN_MINUTE = 60000;
long day, hour, minute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textViewx);
set = (Button) findViewById(R.id.setting);
tv.setText("ONCREATE!");
//LOAD DATA
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
long loadedTime = prefs.getLong("quitDate", 0); //Load Settings Data
boolean test = prefs.getBoolean("test", false);
//get Comparison Data
DateTime newDate = new DateTime();
long updateTime = newDate.getMillis();
long diffInMillis = (updateTime-loadedTime);
//Calculate
minute = (diffInMillis/MILLIS_IN_MINUTE)%60;
hour = (diffInMillis/MILLIS_IN_HOUR)%24;
day = (diffInMillis/MILLIS_IN_DAY);
tv.setText(""+Long.toString(loadedTime));
if(test==true){
tv.setText("" + "\nDay|Hour|Minute: " + Long.toString(day) +":" + Long.toString(hour) + ":" + Long.toString(minute));
}
set.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent x = new Intent(MainActivity.this, Settings.class);
startActivity(x);
}
});
Settings:
import org.joda.time.DateTime;
import android.content.SharedPreferences;
public class Settings extends Activity implements View.OnClickListener {
Button bAbout,bSettings,bFacebook;
//(5000);
final long MILLIS_IN_DAY = 86400000;
final long MILLIS_IN_HOUR = 3600000;
final long MILLIS_IN_MINUTE = 60000;
//Variables
long loadedTime;
int packs;
float cost;
long day, hour, minute, second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Settings");
setContentView(R.layout.activity_settings);
setUpButtons();
DateTime d1 = new DateTime(2013,9,11,0,0);
long today = d1.getMillis();
//SAVE DATA
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putLong("quitDate", today);
editor.putBoolean("test", true);
editor.commit();
minute = (today/MILLIS_IN_MINUTE)%60;
hour = (today/MILLIS_IN_HOUR)%24;
day = (today/MILLIS_IN_DAY);
TextView tv = (TextView) findViewById(R.id.TEMPDATE);
TextView tv2 = (TextView) findViewById(R.id.todayis);
tv2.setText("Today is: " + Long.toString(today));
}
You are using preferences that are private to each Activity. This means you have two sets of preferences: one for MainActivity and one for Settings.
Here is the relevant documentation snippet:
Activity persistent state is managed with the method getPreferences(int), allowing you to retrieve and modify a set of name/value pairs associated with the activity. To use preferences that are shared across multiple application components (activities, receivers, services, providers), you can use the underlying Context.getSharedPreferences() method to retrieve a preferences object stored under a specific name
http://developer.android.com/reference/android/app/Activity.html#getPreferences(int)
You could use something like getSharedPreferences("MY_PREFERENCES", MODE_PRIVATE)
or just call PreferenceManager.getDefaultSharedPreferences(Context context)
to get the default shared preferences set.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.