0

How can I save a variable that is passed from an activity to a table layout in another Activity using Shared preferences, for example, a list of items and it's prices

1. Dread Lock Loosen 0.70

2. Dread Lock Removing 0.45

I want to make these data persist or to be saved even if I close the app

Jasurbek
  • 2,946
  • 3
  • 20
  • 37
  • these are key-value pairs so [sharedpreferences](https://stackoverflow.com/questions/23024831/android-shared-preferences-example) will be a good option – suvojit_007 Jun 16 '19 at 02:36

1 Answers1

0

Hope this will help you,

// Good way to use shared preference is like this

public class AppSharedPreference {

public static final String ABC_APP_SHARED_PREFERENCES = "com.example.bachatgatapp.sharedpreferences"; ///////Agent Details need to store in shared preferences///////

private static final String MOBILE_FILE = "mobfile";

// SET - GET "MOBILE NUMBER" IN SHARED PREFERENCE

public static void setMobileFile(Context context, String value) { SharedPreferences preferences = context.getSharedPreferences(ABC_APP_SHARED_PREFERENCES, Context.MODE_PRIVATE);

preferences.edit().putString(MOBILE_FILE, value).commit(); }

public static String getMobileFile(Context context) {

SharedPreferences preferences = context.getSharedPreferences(ABC_APP_SHARED_PREFERENCES, Context.MODE_PRIVATE);

return preferences.getString(MOBILE_FILE, ""); }

// Write this code to save value in shared preference

AppSharedPreference.setMobileFile(this, mobile_number);

SaiAbhijit
  • 386
  • 4
  • 6
  • Sir please assuming i am having a table layout consisting of rows , how will these code save my values passed to it ,so that when i close the app my values will still be present in my table layout . @user6318595 – Johnny-wycliffe Jun 17 '19 at 03:32