0

I have a problem with with sharing data between two different activities. I have data like :

 int number
 String name 
 int number_2
 int time
 int total

I'm trying to make something like order list with this set of data . So it will take one set of data , then back to previous activity , move forward and again add data to it .

I have an idea of making it in array of object - but data inside was cleared after changing activity.

How can I make it ?

I don't know if and how to add Array of object to SharedPreferences , and get value of one element from there.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
matgod
  • 49
  • 2
  • 12

3 Answers3

1

You should have a look at the documentation of the Intent(s) if you want to do that on the fly associating a key to the value(s) that you want to pass to your second activity.

Anyway, you can think any(sharedpref, database,...) way to pass your parameters but for those kind of things it's a convention and a good practice to follow that.

Simone Casagranda
  • 1,217
  • 17
  • 26
0

Why not to use Intents

Intent intent = new Intent(FirstActivity.this, (destination activity)SecondActivity.class);
intent.putExtra("some_key", value);
intent.putExtra("some_other_key", "a value");
startActivity(intent);

in the second activity

Bundle bundle = getIntent().getExtras();
int value = bundle.getInt("some_key");
String value2 = bundle.getString("some_other_key");

EDIT if you want to read more about adding array to shared preferences check this

Is it possible to add an array or object to SharedPreferences on Android

also this

http://www.sherif.mobi/2012/05/string-arrays-and-object-arrays-in.html

Community
  • 1
  • 1
Android
  • 427
  • 5
  • 14
0

Don't used share preferences for this...Use the singleton pattern, extend Application, or just make a class with static variables and update them...

You can use .putExtra but since you are communicating with more than one activity the above suggestions are probably the best.

public class ShareData {
private String s;
private int s;
private static ShareData shareData = new ShareData();
private ShareData(){}

public static ShareData getInstance(){ return shareData} 
//create getters and setters;
}
horvste
  • 636
  • 6
  • 19
  • Why the downvote, does the answer not pertain to the question? The OP wants to pass data between two activities, not save data after the application closes. – horvste Jan 14 '14 at 22:58
  • Because it's one of the thing that should be avoided. It works, but it's not the conventional way for the Android platform to exchange values between Activities. – Simone Casagranda Jan 14 '14 at 23:20
  • @SimoneCasagranda What is the conventional way? – horvste Jan 14 '14 at 23:27
  • The way in which you should exchange content between activities for the purposes of the question fits perfectly with the data associated with Intents. In this way you can have for example some data back into the activity callback onActivityResult, from the received intent you can check the input,... using a singleton in my opinion is only a black box that don't allow you to be sure to have something and that the value is the most recent,... – Simone Casagranda Jan 14 '14 at 23:31
  • I kind of disagree with the conventional part. Using the singleton pattern is part of the android developer docs http://developer.android.com/guide/faq/framework.html#3 – horvste Jan 14 '14 at 23:41
  • Ok. How do you know in the second activity that some values have been set or that is something ready to be manage? You don't have any control on that. And if you read more in deep there's written in the same application object. Intents from this point of view offer more flexibility. – Simone Casagranda Jan 14 '14 at 23:47