0

I have 3 activities. I'm trying to get a string value from the first Activity to the second Activity. And then I want both the string value of the first Activity and the second Activity in the third activity.

How should the code look in my activities to achieve this?

My flow is first I execute the first Activity, then in the first I start the second Activity, and finally in the second I start the third Activity.

Any help would be appreciated.

Alex DiCarlo
  • 4,851
  • 18
  • 34
Make it Simple
  • 1,832
  • 5
  • 32
  • 57

5 Answers5

1

please using Intent to put string like :

Intent i=new Intent(Activity1.this,Activity2.class);
i.putExtra("string1",//here add your string);

and same code paste second activity to pass two string like:

String test=getIntent.getStringExtras("string1");

Intent i=new Intent(Activity2.this,Activity3.class);
i.putExtra("string1",test);
i.putExtra("string2",//here add your string);

and same as get two value to activity3 like:

 String test=getIntent.getStringExtras("string1");
 String test2=getIntent.getStringExtras("string2");
Mahesh Kavathiya
  • 573
  • 5
  • 23
1

You can add extras to an intent, for Activity1, use the following snippet:

Intent intent = new Intent(this, Activity2.class);
intent.putExtra("SuperSecretValue", /* put your string here */);
startActivity(intent);

In Activity2, use the following snippet:

String superSecretValue = getIntent().getStringExtra("SuperSecretValue");
Intent intent = new Intent(this, Activity3.class);
intent.putExtra("SuperSecretValue", superSecretValue);
intent.putExtra("AnotherSuperSecretValue", /* put your string here */);
startActivity(intent);

In Activity3, use the following snippet:

String superSecretValue = getIntent().getStringExtra("SuperSecretValue");
String anotherSuperSecretValue = getIntent().getStringExtra("AnotherSuperSecretValue");

Check the documentation for more information on what you can do with intents. For this specific case, you can add extra key, value pairs (called "extras") to the intent object that can be retrieved by name in the activity that the intent object is used to start.

Alex DiCarlo
  • 4,851
  • 18
  • 34
  • what is that SuperSecretValue and AnotherSuperSecretValue can u explain im new to android – Make it Simple Jan 12 '13 at 05:44
  • @OneManArmy I updated my answer with an explanation. "SuperSecretValue" is just the name of the key I used to associate with the first value, and "AnotherSuperSecretValue" is just the key I used to associate with the second. I then used those names to retrieve the values I stored in the intent object. – Alex DiCarlo Jan 12 '13 at 05:48
  • Im string is tid,lid but it gets the int value from database based upon the int value im getting the string.. same code is use for this – Make it Simple Jan 12 '13 at 05:56
  • If you are able to get values from one activity to another, please accept this answer and create another question with your new issue. I'd rather not try to answer a question in a comment thread, and it's best not to edit the original question. – Alex DiCarlo Jan 12 '13 at 05:58
  • no i didnt get the data.. i given the same SuperSecretValue and AnotherSuperSecretValue for this string i gave and i give in system .out... to see im getting i didnt get showing null for one value – Make it Simple Jan 12 '13 at 06:29
1

You should make an Intent and then use the the method putExtra.

Here is an example.

To set your variable in activity1 to be passed to activity2

String yourString = "Hello World";
Intent intent = new Intent(activity1.this,activity2.class);
intent.putExtra("RandomName",yourString);
startActivity(intent);

Now for activity 2

Intent getIntent = getIntent();
String passedString = getIntent.getString("RandomName");

Repeat for your third activity.

You can read more Here

yarakyo
  • 499
  • 3
  • 6
  • Im string is tid,lid but it gets the int value from database based upon the int value im getting the string.. same code is use for this – Make it Simple Jan 12 '13 at 05:58
  • I am not sure of the problem you are having, if you need to put pass a Integer to the next activity you an do it with intent.putExtra and get it with getIntent.getInteger – yarakyo Jan 12 '13 at 06:06
  • from database im getting the id of the data through id only im getting the data. the id is int. if pass the id and get id i cant get the data. the is string... – Make it Simple Jan 12 '13 at 06:48
0

the activities must be created first to have values... passing values with intent is like this

Intent i = new Intent("my.activity.TWO");
i.putExtra("extraString1", myString);
startActivity(i);

then in Act Two.

Intent geti = getIntent();
    Bundle b = geti.getExtras();
    grade = b.getString("extraString1");
mcr619619
  • 435
  • 1
  • 4
  • 13
0

By using Bundle concept you can achieve this...

Example: Sending data from MainActivity to Next page for here Secondage

Intent i=new Intent("android.intent.action.Secondpage.class");

                Bundle b=new Bundle();
                b.putString("key1",ed1.getText().toString());
                b.putString("key2",ed2.getText().toString());
                b.putString("key3",ed3.getText().toString());
                int m=Integer.valueOf(ed4.getText().toString());
                b.putInt("key4",m);
                int n=Integer.valueOf(ed5.getText().toString());
                b.putInt("key5",n);
                i.putExtras(b);
                startActivity(i);


            }
        });

In your SecondPage you can receive this value getIntent().getExtras() function

  Bundle b=getIntent().getExtras();

           String s1=b.getString("key1");
           String s2=b.getString("key2");
           String s3=b.getString("key3");
           int s4=b.getInt("key4");
           int s5=b.getInt("key5");

           tv1.setText(s1);
           tv2.setText(s2);
           tv3.setText(s3);
           tv4.setText(""+s4);
           tv5.setText(""+s5);

This will definitely help you try this

yogi
  • 237
  • 2
  • 12