Intents in Android-Multiscreen App
One activity in an app is specified as the “main” activity which is shown to the user when the app is launched.
Table of Contents
Intent
Whenever a new activity starts, the previous activity is stopped but the system preserves the activity in a stack.
This way when a new activity starts, that new activity is pushed onto the back stack and takes user focus.
Types of intent
An Intent can be of two types:
- Implicit Intent: The target component is not known, and we have a general action to perform.
- Explicit Intent: The target of the intent is known and a fully classified class name of a specific activity is also known.
Explicit Intent
We will now create an app that has an EditText on the first activity. The user enters a message and clicks a button to send this message to the second activity.
First Activity Layout(activity_main.xml):
Second Activity Layout(activity_order.xml)
Add an intent using the following code in the method which handle the onClick event of the Button.
Kotlin:
val intent=(this,SecondActivity::class.java)
startActivity(intent)
Java:
Intent intent=new Intent(MainActivity.this,SecondActivity.class); startActivity(intent);
Sending data cross activity
We can send data cross activities using intent extras.
Intent extras are key/value pairs in a Bundle.
A Bundle is a collection of data stored as key/value
intent.putExtra("key","value");
We can get this message from another activity using:
Kotlin:
val intent=getIntent()
val msg=intent.getStringExtra("key")
Java:
Intent intent=getIntent(); String msg=intent.getStringExtra("key");
Kotlin:
MainActivity.kt
class MainActivity : AppCompatActivity() {
val const MSG = "rishiz.site.multi_screen_order"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
addListnerOnButton()
}
private fun addListnerOnButton() {
val btnPlaceOrder = findViewById<Button>(R.id.button)
btnPlaceOrder.setOnClickListener {
val intent = Intent(this, OrderActivity::class.java)
val editText1 = findViewById<EditText>(R.id.order1)
val editText2 = findViewById<EditText>(R.id.order2)
val message = "Order for " + editText1.text + " & " + editText2.text + " is Placed"
intent.putExtra("key")
startActivity(intent)
}
}
}
OrderActivity.kt
class OrderActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_order)
val intent = intent
val msg = intent.getStringExtra(MainActivity.MSG)
val textview = findViewById<TextView>(R.id.placeOrder)
textview.text = msg
}
}
Main_activity.java
Order_activity.java
To navigate back from the OrderActivity(child activity) to the MainActivity (parent activity) you have write a simple line in AndroidManifest.xml file :
Multiscreen App
Implicit intent
In implicit intent, we initiate activity without knowing which app or activity will handle the task.
Ex: Take a photo, open this URL, etc.
Activities with matching intent filters opt in to perform the action.
The following code starts an activity to open a URL.
Kotlin:
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("\"https://rishiz.site/")
startActivity(intent)
Java:
String url="url"//https://rishiz.site/ Intent intent=newIntent(Intent.ACTION_VIEW,webpage); if(intent.resolveActivity(getPackageManager())!=null){ startActivity(intent); } else{ //cannot handle intent }
Similarly, we can handle intents with other actions.
For e.g. open a location, share text, etc.
See android docs for more.
Intent Filter
An intent filter is an expression in an app’s manifest file that specifies the type of intents that the component would like to receive. For instance, by declaring an intent filter for an activity, you make it possible for other apps to directly start your activity with a certain kind of intent.
Likewise, if you do not declare any intent filters for an activity, then it can be started only with an explicit intent.
Caution: To ensure that your app is secure, always use an explicit intent when starting a Service and do not declare intent filters for your services. Using an implicit intent to start a service is a security hazard because you can’t be certain what service will respond to the intent, and the user can’t see which service starts. Beginning with Android 5.0 (API level 21), the system throws an exception if you call bindService() with an implicit intent.
[…] Next: Intent in Android-MultiScreen App […]