I have an activity that is defined in the manifest as:
android:launchMode="singleInstance"
Now I want to move to that activity from a different activity that has no idea that it exists (no reference to the existing instance) and at the same time to pass in a variable. I used to do this like so (before I defined it as singleInstance):
Intent intent = new Intent(this, receivingactivity.class);
intent.putExtra("somekey", somevalue);
startActivity(intent);
But now that doesn't work anymore. I put in the receiving activity the following lines in onresume:
String somevalue = getIntent().getStringExtra("somekey");
and it returns null. How can I pass a value to the existing receiving activity (which is always active and never gets to ondestroy but maximum to onpause)?
We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.
You can pass the value in 2 ways: Either you make a global Class and set the value in that class and access that class in your 3rd activity. You can use Intent to send your values from 1st activity to 2nd activity.
To declare your activity, open your manifest file and add an <activity> element as a child of the <application> element. For example: <manifest ... > The only required attribute for this element is android:name, which specifies the class name of the activity.
You need to use onNewIntent(Intent intent)
to retrieve intent passed to it if the activity's launch mode is singleInstance and provided it's not destroyed.
Do not be confused with getIntent()
for this one retrieves intent passed on the activity's creation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With