Skip to content
Fix Code Error

How to use putExtra() and getExtra() for string data

March 13, 2021 by Code Error
Posted By: Anonymous

Can someone please tell me how exactly to use getExtra() and putExtra() for intents? Actually I have a string variable, say str, which stores some string data. Now, I want to send this data from one activity to another activity.

  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 is very basic question but unfortunately I am stuck here.
Please help.

Thanks,

Edit: Here the string which I am trying to pass from one screen to the other is dynamic.
That is I have an editText where I am getting string whatever user types. Then with the help of myEditText.getText().toString() . I am getting the entered value as a string then I have to pass this data.

Solution

Use this to “put” the file…

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

Then, to retrieve the value try something like:

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");
}
Answered By: Anonymous

Related Articles

  • Unable to run Robolectric and Espresso with a…
  • How to pass values between Fragments
  • Update json data from a python file
  • how to show progress bar(circle) in an activity having a…
  • MaterialCardView is in front of NavigationView. How can I…
  • Android app unable to start activity componentinfo
  • How can I resolve Web Component Testing error?
  • How to filter specific apps for ACTION_SEND intent (and set…
  • Android + Pair devices via bluetooth programmatically
  • How to filter a RecyclerView with a SearchView

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to read a large file – line by line?

Next Post:

How to escape special characters in building a JSON string?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error