Skip to content

Instantly share code, notes, and snippets.

@kohendrix
Last active June 17, 2021 02:30
Show Gist options
  • Select an option

  • Save kohendrix/d14b20ec319899150d940299dd75bd88 to your computer and use it in GitHub Desktop.

Select an option

Save kohendrix/d14b20ec319899150d940299dd75bd88 to your computer and use it in GitHub Desktop.
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import com.example.koheiando.reprosample.DeepLinkTargetTWOActivity.Companion.EXTRA_TEXT
/**
* does not have UI, just start the right activity requested from a browser
*/
class DeepLinkNavigatorActivity : AppCompatActivity() {
companion object {
private val TAG = DeepLinkNavigatorActivity::class.java.simpleName
private const val DL_TARGET_ONE_PATH = "targetONE"
private const val DL_TARGET_TWO_PATH = "targetTWO"
private const val QUERY_TEXT = "text"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
intent.data?.let {
when (it.lastPathSegment) {
DL_TARGET_ONE_PATH -> toTargetONE()
DL_TARGET_TWO_PATH -> toTargetTWO(it)
}
}
}
override fun onResume() {
super.onResume()
finish()
}
private fun toTargetONE() {
startActivity(Intent(this, DeepLinkTargetONEActivity::class.java))
}
/**
* if the query param is not passed, it won't do anything
*/
private fun toTargetTWO(uri: Uri) {
uri.getQueryParameter(QUERY_TEXT)?.let {
startActivity(Intent(this, DeepLinkTargetTWOActivity::class.java).apply {
putExtra(EXTRA_TEXT, it)
})
} ?: Log.w(TAG, "target ${DeepLinkTargetTWOActivity::class.java.simpleName}, uri $uri, query param is not passed. not starting the activity")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment