1

Ok So i want to pass values from One activity to another using getextrand putextra method.

In Second Activity in which I want to receive data is full of contents like Buttons and Text view. and i want to set that certain value which i have received from MainActivity to a particular text box.

setContenView(R.id.intent)

is the easiest one method to show a string but what if I want to Set this value to one or more textview. My code is here

MainActivity

package com.prashant.cookbook;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity {


static String Message_send="Prashant";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText et=(EditText)findViewById(R.id.editText1);
    Button send=(Button)findViewById(R.id.send);

    final Intent msg_send= new Intent(this,Second.class);
    String MSG= et.getText().toString();
    msg_send.putExtra(Message_send, MSG);
    send.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            startActivity(msg_send);


        }
    });


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

SecondAvtivity

package com.prashant.cookbook;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class Second extends Activity {

private TextView tv;
private Intent rcv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    tv = (TextView) findViewById(R.id.msg_show);
    rcv = getIntent();
    String Show_msg;
    Show_msg=rcv.getStringExtra(MainActivity.Message_send);
    tv.setText(Show_msg);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.second, menu);
    return true;
}

}

but when I run this code I got nothing but a blank second Activity Not even a default text

Prashant Patel
  • 238
  • 2
  • 12

1 Answers1

2

get Value entered by user in EditText on Button click and then use msg_send.putExtra for placing value in Intent as:

send.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

       String MSG= et.getText().toString();  //<< get value from EditText here
       msg_send.putExtra(Message_send, MSG);            
       startActivity(msg_send);
    }
});
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Is using a `static` `String` and accessing it this way like `getIntent().getStringExtra(MainActivity.SomeStaticString);` a good idea? I've seen people do it but I never thought it seemed safe... – codeMagic Oct 17 '13 at 19:07
  • 2
    @codeMagic yes it is. it improves code readability and it less errors prone – Blackbelt Oct 17 '13 at 19:14
  • 1
    @blackbelt ok, thanks for the response. I guess I was thinking you wouldn't have a reference to it if the `Activity` is finished... – codeMagic Oct 17 '13 at 19:17
  • 1
    @codeMagic a static field in java lives as long as the process of the application lives – Blackbelt Oct 17 '13 at 19:21
  • 1
    @blackbelt I knew with Java that is how it works. I guess I thought that it was different within the Android framework for an `Activity`. Not sure why I thought that but thanks for clearing it up! I guess I will start doing it that way from now on. – codeMagic Oct 17 '13 at 19:23
  • 1
    @codeMagic http://stackoverflow.com/questions/2475978/using-static-variables-in-android this should help – Raghunandan Oct 18 '13 at 15:54
  • @Raghunandan thanks for the link! That helps. I need to learn more about Singleton because I've never used it. Maybe not good that I have gotten this far without understanding it yet :/ – codeMagic Oct 18 '13 at 15:59