1

enter image description here

look at image link above, how to code that simple passing data between 3 activities?

Activity 1:

  • input name

  • then click insert button

  • then intent to Activity 2

Activity 2:

  • click show button

  • then intent to Activity 3

Activity 3:

  • setText the input name from Activity 1

Maybe i should have use setter getter? but how?

I need this for learning the basic, thanks :)

fredy wijaya
  • 15
  • 1
  • 5

3 Answers3

2

There are many ways , but here is one,

When ever your moving from Activity A to Activity B (probably on button click) pass the inputName to B activity

ActivityA.java

Intent i = new Intent(this, ActivityB.class);
i.putExtra("DATA", "inputName");
startActivity(i);

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

So the info u pass from A to B you need to check and pass it to C while moving from B to C

Intent newIntent = new Intent(ActivityB.this, ActivityC.class);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    newIntent.putExtras(bundle);
}
startActivity(newIntent);

In Activity C you can get the data like this and use

Bundle b = getIntent().getExtras();
String data1 = b.getString("DATA");

This is one simple solution and there are many other ways you can try like

  • Storing in Shared pref and retrieving
  • Using application class
  • Using some interface etc but in your case this simple solution is enough i guess
NatuGadu
  • 53
  • 1
  • 9
1

In Activity 1:on click listener of insert button put this:

   Intent  i = new Intent(Activity1.this, Activity2.class);
   String name = input.gettext();
   i.putExtra("name",name);
   StartActivity(i);

above code will pass value from activity 1 to activity 2,i assume you are quite new to android development go through this link carefully to understand Intents and starting activity

Now in Activity2 get the values from intent

  String name=getIntent().getStringExtra("name");

name will have value passed from activity 1;

Now following same pattern you can pass value from activity 2 to activity 3

DevKRos
  • 422
  • 2
  • 15
0

use

 intent.putExtra("name","value");
Rajkumar Kumawat
  • 290
  • 2
  • 11