Issue
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.
Solution
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);
}
Answered By - Sree Reddy Menon
Answer Checked By - Timothy Miller (JavaFixing Admin)