我想要保存timestamp變量中的時間戳,但總是處於活動狀態,並且timestamp變量的值發生變化。爲什麼在Android如果/否則總是其他語句激活
String timestamp = "";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_product);
if (!timestamp.equals("")) {
preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
timestamp= preferences.getString("timestamp", "");
Toast.makeText(getApplicationContext(), timestamp + "iffff", Toast.LENGTH_LONG).show();
} else {
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("timestamp", ts);
editor.commit();
timestamp = preferences.getString("timestamp", "");
Toast.makeText(getApplicationContext(), timestamp + "elseee", Toast.LENGTH_LONG).show();
}
}
非常感謝。 – Saeidhp