Home > Software design >  Android SharedPreferences check for empty or null
Android SharedPreferences check for empty or null

Time:10-12

In my android application I am saving the last order did in Sharedpreference. When the user check for last order I am calling Sharedprefernce and showing it. That is working perfectly. But if nothing is saved in shared preference app is crashing. So I would like to check whther sharedpreference has any values saved or not first.

String lastOrder = sharedPreferences.getString("orderNO", "");

CodePudding user response:

you just need to check isEmpty of you sharedPreference value before setting it

   String lastOrder = sharedPreferences.getString("orderNO", "");

   if (!lastOrder.equals("")){
        orderField.setText(lastOrder);
    }
  • Related