How to use already running app while sharing image from inside the gallery app? It is always creating separate instance of already running app. I have observed same problem in whatsapp application.
-
it´s unclear what You ´re asking. Please be more specific....if possible code examples, describtion what exactly should happen and what does instead, may screenshot etc..... – Opiatefuchs Feb 19 '16 at 13:43
-
the scenario : first launching my app and kept running in background then launching gallery app separately >select one image and click share >choosing my app – Bishwajeet Biswas Feb 19 '16 at 15:19
-
you can see in whatsapp application also. – Bishwajeet Biswas Feb 19 '16 at 15:21
1 Answers
yes.another usecase is when you are clicking on notification.it will start a new instance if the app is already in background.
using android:launchMode
<activity android:launchMode = ["standard" | "singleTop" | "singleTask" | "singleInstance"] ../>
so using "singleTop"
From the docs:
If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is not an existing instance of the activity).
please read below blog posts
http://inthecheesefactory.com/blog/understand-android-activity-launchmode/en
https://www.mobomo.com/2011/06/android-understanding-activity-launchmode/
Edited: Before OnResume , It will call OnActivityResult. with data that has been choosen.
Intent intent = new Intent();
intent.setType("image/jpeg");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Image From Gallery"),
10000);
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri originalUri = null;
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 10000) {
if (data.getData() != null) {
originalUri = data.getData();
//originaluri is your selected image path.
}}}
and for getting Intent Filter Action in our app.
void onCreate (Bundle savedInstanceState) {
...
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
} else if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
}
http://developer.android.com/training/sharing/receive.html http://developer.android.com/training/basics/intents/filters.html
or if you are using launchmode- singleTop. just override onNewIntent.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}

- 30,962
- 25
- 85
- 135

- 1,301
- 13
- 23
-
Thanks, Launchmode is useful when getting existing same background app in foreground, not for getting intent value(selected image path) in case of sharing image from inside the gallery app, because it is calling onResume method of that running activity....... – Bishwajeet Biswas Feb 19 '16 at 14:34
-
-
if activity created first time then it is possible to get intent value in onCreate and onResume method – Bishwajeet Biswas Feb 19 '16 at 14:41
-
Hey I have updated the answer.please check. and You can get intent value anywhere using getIntent().getExtras().get("value"); – Sree Reddy Menon Feb 19 '16 at 14:47
-
thanks, before onResume ,onActivityResult is called if use startActivityforResult for that activity – Bishwajeet Biswas Feb 19 '16 at 14:51
-
Yes bro! if my answer solved your problem. please accept the answer – Sree Reddy Menon Feb 19 '16 at 14:54
-
bro, actually i have already done as per your code but intent value is getting null – Bishwajeet Biswas Feb 19 '16 at 14:55
-
what exactly you are looking for ? are launching a gallery app of the device or. you want a gallery inside your app. – Sree Reddy Menon Feb 19 '16 at 14:57
-
-
separately opening gallery app and sharing image using my app. in this case how to get intent data ? – Bishwajeet Biswas Feb 19 '16 at 15:02
-
please paste your code. its unclear for me to understand. are you getting null value for Uri originalUri ? – Sree Reddy Menon Feb 19 '16 at 15:02
-
-
the scenario : first opening my app and kept running in background then run gallery app separately >select one image and click share >choosing my app thats all – Bishwajeet Biswas Feb 19 '16 at 15:09
-
-
Okey,cool. please accept the answer .SingleTask. it will only allow you run one instance everytime. – Sree Reddy Menon Feb 19 '16 at 15:40