vendredi 21 juillet 2017

Why in android If/Else statment always else is activating

I want save timestamp in timestamp variable, but always else active and the value of timestamp variable change.

    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();

    }

}

Aucun commentaire:

Enregistrer un commentaire