Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - pass data to singleInstance activity

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)?

like image 569
Jon Avatar asked May 07 '15 14:05

Jon


People also ask

How do I pass data between activities in android?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How pass data from one activity to third activity?

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.

How can we add activity in manifest?

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.


1 Answers

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.

like image 97
inmyth Avatar answered Sep 20 '22 12:09

inmyth