-4

Can you tell me how putExtra() works? How and where does it stores the data passed to it ? I am still confused how the data inside it works and how it is passed from one activity to other activity? How its structure is organised? Or the values stored on the inside of the Intent class ?

Imagine there is class A. it has one static string Example.I want to fill this string in next activity so I call activityforresult()

Now there is class B.I filled textbox with id text_entered. and I put the code

Intent text = new Intent();
test.putExtra(A.Example, text_entered);

Now where will be text saved, in A example or in intent test?

Sree
  • 1,469
  • 3
  • 15
  • 36
  • 1
    possible duplicate of [How to use putExtra() and getExtra() for string data](http://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data) –  May 12 '15 at 04:37
  • 1
    You must see android source code then – N J May 12 '15 at 04:42
  • Before post a question in stack overflow, do google it first – Madhu May 12 '15 at 04:59

3 Answers3

1

Activity1.class

Intent intent = new Intent(getApplicationContext(), Activity2.class);
    intent.putExtra("EXTRA_NAME", 4);//Int
    intent.putExtra("EXTRA_NAME", "String_Values");//String

Activity2.class

String stringValue = getIntent().getStringExtra("EXTRA_NAME");//String extra
    int iValue = getIntent().getIntExtra("EXTRA_NAME", 0);//defaultValue = 0
Nam Hoang
  • 46
  • 3
0

Your text will be carried along with the intent to the other activity as described by @NamHoang in his answer. The A.Example (should be a string) in your case is just a key that is used to refer the value present in the intent. I hope you know that it is the key-value pair that is being sent along with the intent.

Ali_Waris
  • 1,944
  • 2
  • 28
  • 46
0

A.class

static String Example = "this is a example string";

private void JumpToB(){
    Intent intent = new Intent();
    intent.putExtra("key_data", Example);
    intent.setClass(this,B.class);
    startActivity(intent);
}

B.class

protected void onCreate(Bundle savedInstanceState) {
    String example = getIntent().getStringExtra("key_data");
    text_entered.setText(example);
}

If you want to pass a Model, a good way is to transform the model to json.

wotakuc
  • 1
  • 1