0

I want to pass a specific String from a fragment to an activity to use it there. When I am using Intent and Bundle, the value is null. I have tried several methods but can't seem to make it correctly. Can someone help, please?

In my Fragment :

Intent testintent = new Intent(getActivity(), MapActivity.class);         
Bundle bundle = new Bundle();         
bundle.putString("testing", "This is a test");         
testintent.putExtras(bundle);`

And in my Activity :

String message = getIntent().getStringExtra("testing");         
System.out.println("Value is : " + message);`

And this is what I am getting as a result in my console :

I/System.out: Value is : null
groff07
  • 2,363
  • 3
  • 32
  • 48
  • Does this answer your question? [Passing data between a fragment and its container activity](https://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity) – groff07 Jan 09 '23 at 22:36
  • Edit your question and show the code you use to launch the `Activity`. – David Wasser Jan 11 '23 at 09:16

2 Answers2

0

In the Fragment :

    val intent = Intent(this, MapsActivity::class.java)
    intent.putExtra("testing", "This is a test")
    startActivity(intent)

In the receiving Activity :

    val extras = intent.extras
    if (extras != null) {
       val  receivingString = extras.getString("testing")
       System.out.println("Value is : " + receivingString)
    } else {
    
    }
harunkor
  • 89
  • 4
0

Using jave:

Intent intent = new Intent(getActivity(), MapActivity.class);
intent.putExtra("testing", "This is a test");
getActivity.startActivity(intent);
Ismail Hamzeh
  • 43
  • 1
  • 6