0

Here is the scenario..

There are 3 activities.. A1 (1st activity), A2 (2nd activity), A3 (3rd activity). In this case the flow of activities should be from A1-> A2 -> A3

In A1, I'm doing the intial data retrieval from the server and would like to send the data to 3rd Activity i.e. to A3 directly ( but i don't want to trigger A3 using A1 )

i.e . when the Activity A3 is launched from A2, i want to use the data sent by A1

The obvious solution (using plain java way) is using a common data structures like Hashmap/List/.. to insert the data from A1 and retrieve this data in A3. I would like to know if there is any other alternate solution using Android API

Thanks

Jitendar M
  • 633
  • 1
  • 10
  • 26

6 Answers6

1

Use SharedPreference. Save in A1 and retrieve in A3.

Initialization

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();

Storing Data

editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long

editor.commit(); // commit changes

Retrieving Data

// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean

Deleting Data

editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes

Clearing Storage

editor.clear();
editor.commit(); // commit changes
fida1989
  • 3,234
  • 1
  • 27
  • 30
0

There are two ways in which you can make your job done.

1)You can pass the values with the help of intent from activity1 to activity2. And then from there again in intent you can pass it to activity3.

2)Or you can initialise your variables(whose values you want in activity3) in activity1 as static and you can make use of them in activity3.

Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

You used two way to connect data for access in different classes...

=> The First way is to pass data with Intent using putExtra() function ..... show here for how to work...

=> The second way is that use one common class and store data in that for using that data from any other classes... show here for how to work....

that both way to connect different class for excess common data...

Community
  • 1
  • 1
Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
0

when u are calling A2 from A1 u can attach the data using putExtra which u can retreive in A2 then again when u are calling A3 from A2 u can sand the data using putExtra. again u can retreive the data using getExtra.

abc
  • 74
  • 9
0

Create the model bean class in your and store your activity 1 values

While opening up activity 3 get the values from model bean class and set the values to activity3

Venkatesh S
  • 5,466
  • 1
  • 25
  • 30
0

you can use Bundle for pasing data form one Activity class to other Activity class Like this

Bundle bundle = new Bundle();
bundle.putString("Id", videoChannelId);
bundle.putString("C/V", "C");
bundle.putString("mode", "ch");
bundle.putString("code", "LiveTV");
bundle.putString("urlcount", "2");
Intent intent = new Intent(First.this,Second.class);
intent.putExtras(bundle);
startActivity(intent);

Get the data in the second Activity like this by giving the bundle id

 String id;
 Bundle  getBundle = this.getIntent().getExtras();

 id= getBundle.getString("Id") 
  etc......
user2691782
  • 145
  • 2
  • 9
  • If I'm transferring data from A1 to A2, your method will work, but my question is to transfer data from A1 to A3 , while following the activities A1->A2->A3 – Jitendar M Aug 22 '13 at 07:24