How do I insert and receive String data as putExtra() and getExtra()?

Asked 1 years ago, Updated 1 years ago, 49 views

Please tell me how to write getExtra() and putExtra().

How I'm going to use it is There is a string variable called str, and I want to save this value. I want to save it and send the data from this activity to another activity, what should I do?

  Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
  String keyIdentifer  = null;
  i.putExtra(strName, keyIdentifer );
and then in the SecondScreen.java

 public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);
        TextView userName = (TextView)findViewById(R.id.userName);
        Bundle bundle = getIntent().getExtras();

        if(bundle.getString("strName")!= = null)
        {
            //TODO here get the string stored in the string variable and do 
            // // setText() on userName 
        }

    }

I know it's a basic question, but I'm stuck here, so I'm asking you this.

android android-intent

2022-09-21 18:14

1 Answers

Create an int to display other activities, as shown below You can "put" the value to be sent to the intent. "STRING_I_NEED" is the key value for receiving the string.

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String strName = null;
i.putExtra("STRING_I_NEED", strName);

And when you get the price, you can do it as below.

String newString;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } } else {
        newString= extras.getString("STRING_I_NEED");
    }
} } else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}


2022-09-21 18:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.