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.
We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.
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);
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.
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);
When calling from B to C, add to your intent:
myIntent.putExtras(getIntent().getExtras());
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With