Skip to content

Instantly share code, notes, and snippets.

@vishna
Created April 16, 2019 11:47
Show Gist options
  • Save vishna/da6e799ca118fcb51f3e21d4d4fd58f2 to your computer and use it in GitHub Desktop.
Save vishna/da6e799ca118fcb51f3e21d4d4fd58f2 to your computer and use it in GitHub Desktop.
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
private const val tag = "2ndShare"
/**
* 1) Clear all the apps
* 2) Go to Google Photos, share image to this app
* 3) Do nothing in this app, switch to Google Photos (…and NOT by navigating back!)
* 4) Share some other image to this app
*
* Change activity mode in manifest to observe what's going on:
* launchMode = standard: ¯\_(ツ)_/¯
* 1st Share -> onCreate,onResume
* 2nd Share -> onResume
*
* launchMode = singleTask: OK
* 1st Share -> onCreate,onResume
* 2nd Share -> onNewIntent,onResume
*
* launchMode = singleTop: OK
* 1st Share -> onCreate,onResume
* 2nd Share -> onNewIntent,onResume
*
* launchMode = singleInstance: OK
* 1st Share -> onCreate,onResume
* 2nd Share -> onNewIntent,onResume
*/
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d(tag, "onCreate ${intent.describe()}")
}
override fun onResume() {
super.onResume()
Log.d(tag, "onResume ${intent.describe()}")
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Log.d(tag, "onNewIntent ${intent.describe()}")
setIntent(intent)
}
}
fun Intent?.describe() : String {
if (this == null) return "null intent"
val extras = extras ?: return "empty intent"
return extras[Intent.EXTRA_STREAM]?.toString() ?: "no stream"
}
@vishna
Copy link
Author

vishna commented Apr 16, 2019

for the standard launch mode, the intent never changes unless you kill the app or finish activity with a result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment