Shared Preferences not passing data between activities
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));
}
No comments:
Post a Comment