0

I am learning android, so I was practising with a program in which I start a new activity with a button and then what ever text was input by the user in the edittext of the previous activity has to displayed in textview of the new started activity but when I pass the user input to the new activity, it doesn't display anything. What can be the problem here is the code: Lets say this parent activity:

        case R.id.Sfr:
        Intent data= new Intent(SendData.this, RecieveData.class);
        Bundle check = new Bundle();

        check.putString("UmerData", cheese);
        medt.setText(cheese);
        data.putExtras(check);
        startActivityForResult(data, 5); // It is used to return data from child activity
        break;

The child activity which should show the user input passed to it is:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recievedata);
    Initialize(); 
    Bundle got = getIntent().getExtras();

    rt1.setText(got.getString("UmerData"));

}

Why is this child activity not showing userinput which has been passed to it?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Alfred James
  • 1,029
  • 6
  • 17
  • 27

3 Answers3

1

Try this -

case R.id.Sfr:
    Intent data= new Intent(SendData.this, RecieveData.class);
    Bundle check = new Bundle();
    check.putString("UmerData", cheese);
    medt.setText(cheese);
    data.putExtras(check);
    startActivity(data);
    break;

The child activity which should show the user input passed to it is:

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recievedata);
    InitializeFuck();
    Bundle got = getIntent().getExtras();
    rt1.setText(got.getString("UmerData"));
}

Have a look at these existing answers -

  1. Passing Data between activities

  2. How to pass the Data between Activities

Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
1

write in first activity:-

Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putStringExtra("keyForData", Value);
startActivity(intent);

write code in second Activity:-

Intent intent = Activity2.this.getIntent();
String data  =intent.getStringExtra("KeyForData");
Sandroid
  • 328
  • 1
  • 10
  • 19
0

1. Use Intent along with putExtra() for sending data to the Another Activity.

Eg:

   Intent i = new Intent(Your_Class.this, Desired_Class.class);

   i.putExtra("NameKey","name");

   startActivity(i);

2. Now use getIntent() on the receiving Activity to get the Starting Intent and then use getExtras() and getString() to get the String value associated with the key. We have getInt() etc too..

Eg:

   Intent intent = getIntent();

   String name = intent.getExtras().getString("NameKey");
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75