I want to get the current system time when I "sync" with another device. So this activity is usually called/displayed right after syncing.
Currently, the current system time is right but I also have alarm pop up another activity and once I'm finished with that activity, I want to go back to this activity. Once I do, the system time is updated to most current time and not the old current time (which is what I want).
I tried to use SavedInstanceState
to save the state of the old current time and while it does get the right string in the onSavedInstanceState
. When I go back to this activity, the SavedInstanceState
is null. I don't know why?
public class InfoActivity extends BaseActivity {
String fileLastSync = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
Resources resources = context.getResources();
if (savedInstanceState != null) {
// Restore value of members from saved state
fileLastSync = savedInstanceState.getString("FileLastSync");
} else {
//Gets the current DateTime
long nextTime = (System.currentTimeMillis());
Date date = new Date(nextTime);
//Formats the current DateTime
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy HH:mm a");
String formatDate = format.format(date);
//Last sync time is the current formatted date
fileLastSync = formatDate;
}
//Gets the 'last synced' string and sets to datetime of the last sync
String syncString = String.format(resources.getString(R.string.last_sync), fileLastSync);
//Dynamically sets the datetime of the last sync string
TextView lastSyncTextView = ((TextView) findViewById(R.id.last_sync) );
lastSyncTextView.setText(syncString);
}
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("FileLastSync",fileLastSync);
super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
// Restore value of members from saved state
fileLastSync = savedInstanceState.getString("FileLastSync");
} else {
//Gets the current DateTime
long nextTime = (System.currentTimeMillis());
Date date = new Date(nextTime);
//Formats the current DateTime
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy hh:mm a");
String formatDate = format.format(date);
//Last sync time is the current formatted date
fileLastSync = formatDate;
}
}
}
EDIT: I tried to put this: super.onSaveInstanceState(savedInstanceState);
at the end of the method but still doesn't work.
Override onRestoreInstanceState(Bundle savedInstanceState)
to restore the date there.
Also, in your onSaveInstanceState
method call 'super.onSaveInstanceState` as the last statement.
You should not put in the same code from onSave into onRestore. Copy the complete if-statement from your onCreate method into the onRestore method instead. And call super.onRestoreInstanceState as the first statement.
Edit: sorry - was intended to be a comment
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.