Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Data Between Multiple Activities

I have three activities, say I have the data from ActivityA, and I want ActivityC to retrieve it but it must go through ActivityB which I don't want it do anything about the passed data. Is there anyway to do it?

I have tried

ActivityA.java

Intent i = new Intent(this, ActivityB.class);
i.putExtra("DATA1", "Hello");
i.putExtra("DATA2", "World");
startActivity(i);

ActivityB.java doesn't do anything about it but it must be seen before ActivityC.

ActivityC.java

Bundle b = getIntent().getExtras();
String data1 = b.getString("DATA1");
String data2 = b.getString("DATA2");

But it gives me this exception

11-30 02:12:05.165: W/dalvikvm(1134): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-30 02:12:05.185: E/AndroidRuntime(1134): FATAL EXCEPTION: main
11-30 02:12:05.185: E/AndroidRuntime(1134): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.src.test/com.src.test.ActivityC}: java.lang.NullPointerException
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.os.Looper.loop(Looper.java:123)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at java.lang.reflect.Method.invoke(Method.java:521)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at dalvik.system.NativeStart.main(Native Method)
11-30 02:12:05.185: E/AndroidRuntime(1134): Caused by: java.lang.NullPointerException
11-30 02:12:05.185: E/AndroidRuntime(1134):     at com.src.test.ActivityC.onCreate(ActivityC.java:98)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-30 02:12:05.185: E/AndroidRuntime(1134):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

which points to String data1 = b.getString("DATA1");

Is there any way to do this correctly?

Any help would be greatly appreciated.

like image 717
src Avatar asked Nov 29 '11 18:11

src


People also ask

How do you pass data into multiple activities?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How can you pass data between multiple activities using intent explain?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity: Intent intent = new Intent(getBaseContext(), SignoutActivity. class); intent. putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);

WHO is also used for transferring data between different activities?

Through Intent we can move from one activity to another activity and Intent can also be used to pass the data from one activity to another activity. In the previous article, we designed the Login Page and now we will learn how to use Intent to pass data from LoginActivity to the next activity.


3 Answers

do this in activity B:

Bundle b = getIntent().getExtras();
String data1 = b.getString("DATA1");
String data2 = b.getString("DATA2");


Intent i = new Intent(this, ActivityC.class);
i.putExtra("DATA1", data1);
i.putExtra("DATA2", data2);
startActivity(i);
like image 28
Pratik Bhat Avatar answered Sep 23 '22 06:09

Pratik Bhat


When calling from B to C, add to your intent:

myIntent.putExtras(getIntent().getExtras());
like image 88
jcxavier Avatar answered Sep 22 '22 06:09

jcxavier


It's because your ActivityB doesn't push it to ActivityC:

Intent newIntent = new Intent(ActivityB.this, ActivityC.class);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    newIntent.putExtras(bundle);
}
startActivity(newIntent);
like image 35
Camille R Avatar answered Sep 22 '22 06:09

Camille R