2

I'm wondering how to pass and read a string that is in one activity from another activity. I have two activities. I'll call them Activity1 and Activity2. I have a string in Activity1 called course. I want to read that string in Activity2.

I've tried doing this but the string came out empty.

public class Activity2 extends Activity1 {

I've seen people use the Intent function but I couldn't figure out how to use it.

Any suggestions? Thanks!

79t97g
  • 105
  • 1
  • 1
  • 9

6 Answers6

7

Pass values using intents.

In your first activity

 Intent i= new Intent("com.example.secondActivity");
 i.putExtra("key",mystring);
 // for explicit intents 
 // Intent i= new Intent(ActivityName.this,SecondActivity.class);    
 // parameter 1 is the key
 // parameter 2 is the value 
 // your value
 startActivity(i);

In your second activity retrieve it.

Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
//get the value based on the key
}

To pass custom objects you can have a look at this link

http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/

Nirav Zaveri
  • 687
  • 1
  • 9
  • 28
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
2

your first activity, Activity1

public class Activity1 extends Activity {
    Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);

        btn=(Button) findViewById(R.id.payBtn);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent=new Intent(Activity1.this,Activity2.class);
                intent.putExtra("course", "courseValue");
                startActivity(intent);
            }
        });
    }
}

Activity2
public class Activity2 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);

        String course=getIntent().getExtras().getString("course").toString();
        Log.d("course",course);
    }
}

Hope this will help you.

Amol Sawant
  • 13,842
  • 2
  • 20
  • 27
1

You're on the right track - you're using an intent to launch the second activity. All you have to do is add intent.putExtra("title", stringObject); where stringObject is the string you want to pass, and title is the name you want to give that object. You use that name to refer the object passed in the second activity as follows:

String s = (String)getIntent().getExtras().getSerializable("title");
drew moore
  • 31,565
  • 17
  • 75
  • 112
  • I seem to be getting this error when I do that. : Cannot make a static reference to the non-static method putExtra(String, String) from the type Intent – 79t97g Apr 07 '13 at 06:11
  • Are you calling putExtra from inside a static method? If so, are you sure you need to be doing so? – drew moore Apr 07 '13 at 06:15
1

From Activity 1 call something like this :

Intent intent= new Intent("path.secondActivity");
intent.putExtra("keyString",sampleString);
startActiivty(intent);

and in activity 2 try something like this :

Bundle values = getIntent().getExtras();
if (values != null) {
    String keyString = values.getString("keyString");
}
lokoko
  • 5,785
  • 5
  • 35
  • 68
1

In your MainActivity

Intent i= new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("key",yourstring);
startActiivty(i);

In your second activity onCreate()

Bundle extras = getIntent().getExtras();
if (extras != null) { 
String value = extras.getString("key");
}
0

Try this

public class Activity2 extends Activity1

Mohan Raj B
  • 1,015
  • 7
  • 14