Which line saves the username in the background without blocking the UI?

  1. prefs.edit().putString("username", n).commit()
  2. prefs.edit().putString("username", n)
  3. prefs.putString("username", n)
  4. prefs.edit().putString("username", n).apply()
Answer

D) prefs.edit().putString("username", n).apply()

apply() writes asynchronously; without apply/commit nothing is saved.

Computer Science MCQ · More Lab: SharedPreferences MCQs · Practise