Intent and Sharing in Android

 Navigation between Activities and Sharing Data  

Android allows us to navigate between activities both within your app, or to the activities provided by other applications. 

Eg. we could launch the system camera activity to have it capture photo for your app or you can launch the contacts app to choose a contact, add, edit etc.. 

All activities must be registered in the Android manifest to be launched. 

Intent and Intent Filters

We use Intent and Intent filters to navigate between Activities and share contents between activities. 

  • Intent is a description of something that the app wants an activity to perform. 
  • Intent are of 2 types : Explicit and Implicit Intent 

Explicit Intent

  • Launch specific activity using the name of the target activity class. 
  • They are typically used to launch other activities within your application. 
  • The navigation component does this for you when you navigate to other activities in the navigation graph. 
  • Activities that are only launched explicitly can be declared with just an activity tag. 

Implicit Intent

  • It provides an abstract description of the operation to be performed. 
  • They are mostly used to launch activities that are exposed by other applications. 
  • When multiple Android Apps can handle the same implicit intent, Android will pop up a chooser like this one that contains a list of compatible apps so that the user can select the desired one to handle the request. 
  • Implicit intents are important because they allow an app to request something from another app without having to know anything about that other app. 
  • When we share some text from our app with an implicit intent, we don't need to know whether the user prefers to share using a social network or a text message or an email, what we just want is to share text with an app that has an activity that can handle the request. 
  • Each implicit Intent must have an action. 
  • Implicit Intent also have a category and datatype to further describe the operation. 
  • Implicitly launched activities require an IntentFilter 

Intent Filter

It is used to expose that your activity can respond to implicit intent with a certain action, category or type. 


Intent Action

  • Action are used in intents to describe the type of thing that is to be done. 
  • Common actions are defined in the Intent class such as View, Edit, Dial 

Intent Category

  • Adds a subtype to the action. 
  • It is not always used. 
  • Eg. Categories are used in main entry point of app to launch an available music player or a mapping action. 

Intent Data Type

  • Allows Activities to support specific data types. 
  • Datatype used such as text or Jpeg image. 

For the Android Launcher to start an activity the activity needs to be registered in Android Manifest with the correct Intent Filter. If the activity do not have this intent filter it would not appear in launcher. 

Use Case:


override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.share_menu, menu)
}

private fun getShareIntent(): Intent {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "text/plain" // "image/JPEG"
shareIntent.putExtra(Intent.EXTRA_TEXT, args?.ans)
return shareIntent
}

private fun shareSuccess(){
startActivity(getShareIntent())
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId){
R.id.share -> shareSuccess()
}
return super.onOptionsItemSelected(item)
}


Sharing is done often so there is a simpler way to generate this using a ShareCompat which helps to do the method chaining and make the code more easily readable. 

private fun getShareIntent(): Intent {
return ShareCompat.IntentBuilder.from(requireActivity())
.setText(getString(R.string.share_success_text))
.setType("text/plain")
.intent
}

Possible Bug Cases

  • If there aren't any compatible activities that support our sharing, the code will crash. 
  • We can easily simulate the crash by just removing the type of intent. 
  • We can handle it in various ways like catching the exception and throwing up a toast message 
  • But we can also handle by hiding the share menu button when the sharing is not possible. 

// check if the activity resolves
if (getShareIntent().resolveActivity(requireActivity().packageManager) == null ){
// so we hide the share menu item
menu?.findItem(R.id.share)?.setVisible(false)
}




















Comments

Popular Posts