0

I have a small android app that I am trying to create and I need to save some values from one activity and pass them back to the main activity. I looked into shared preferences and I couldn't get it to work. I am not sure if that is the best method, I have a class that I would prefer to store the data in but I am not sure how to access a single instance of a class between multiple classes so I was going to go with the shared preferences method.

Here a method I have in my second activity that I thought would work but doesn't.

public void saveInfo(View view){
        SharedPreferences sharedPref = getSharedPreferences("PatientData", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor  = sharedPref.edit();
        editor.putInt("Auditory-1stepq1", auditory1stepQ1);
        editor.apply();
        System.out.format("auditory1stepQ1: %I", auditory1stepQ1);
        Toast.makeText(this, "saved!", Toast.LENGTH_SHORT).show();
    }

Does anyone know a simple method for storing data between multiple classes using Android resources, I would do a flat file it was easy enough.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Cory Hall
  • 39
  • 5
  • What makes you think that it doesn't work? How did you check that? – Sergey Emeliyanov Dec 19 '17 at 06:55
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – ADM Dec 19 '17 at 07:15
  • @Serj Ardovic I used The Android Device monitor and found the .xml file with only 1 value so it does seem to be saving something just not everything. I had five values and only 1 of them is being saved. – Cory Hall Dec 19 '17 at 07:58
  • Well, according to the code you've posted, you are saving only ONE value. – Sergey Emeliyanov Dec 19 '17 at 08:00
  • This is true but I've modified it since the post and when I looked at it, I have 5 variables. – Cory Hall Dec 19 '17 at 08:05
  • I ended up using the default shared preferences and that seems to work well. – Cory Hall Dec 20 '17 at 09:19

1 Answers1

0

you can use TinyDb, just import java file in your project for this it's work for me.

here is the link:

https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo

sample code:

TinyDB tinydb = new TinyDB(context);

//Put data in database
tinydb.putInt("clickCount", 2);
tinydb.putFloat("xPoint", 3.6f);
tinydb.putLong("userCount", 39832L);

tinydb.putString("userName", "john");
tinydb.putBoolean("isUserMale", true); 

tinydb.putList("MyUsers", mUsersArray);
tinydb.putImagePNG("DropBox/WorkImages", "MeAtlunch.png", lunchBitmap);

//Get data from database
int dataint = tinyDB.getInt("clickCount");
String datastring = tinyDB.getString("userName");
Tara
  • 692
  • 5
  • 23