Pre-requisites:
This article aims to explain Implicit and Explicit intents and how to use them in an Android app.
The concept of intent in Android revolves around a messaging object that facilitates communication between various components such as services, content providers, and activities. Typically, the startActivity() method is employed to trigger the launch of any activity. Here are some fundamental functions of an intent:
Method | Description |
Context.startActivity() | This method is utilized to either initiate a new activity or bring an existing activity into action. |
Context.startService() | It is used to commence a new service or convey instructions to an already existing service. |
Context.sendBroadcast() | With this method, messages can be broadcasted to broadcast receivers. |
Implicit Intents do not specify components directly. Instead, they declare an action to be performed. The Android operating system then filters out components capable of responding to the action. For instance, consider the scenario where a webpage needs to be opened. By typing the desired webpage name and clicking on the 'CLICK' button, the webpage is successfully accessed.
To initiate a fresh project in Android Studio, follow the guidelines in setting up a new project. The instructions are provided in Java and Kotlin programming languages for Android. Generate an XML file and a Java File. It is recommended to review the prerequisites for a better understanding of this phase.
Proceed to the activity_main.xml file, which visually represents the user interface (UI) of the project. Below is an excerpt of the code within the activity_main.xml file. Embedded comments within the code aim to facilitate a deeper comprehension of the code structure.
<?xml version=\"1.0\" encoding=\"utf-8\"?> |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" xmlns:app=\"http://schemas.android.com/apk/res-auto\" xmlns:tools=\"http://schemas.android.com/tools\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" tools:context=\".MainActivity\"> |
<EditText android:id=\"@id/editText\" android:layout_width=\"match_parent\" android:layout_height=\"wrap_content\" app:layout_constraintBottom_toBottomOf=\"parent\" app:layout_constraintEnd_toEndOf=\"parent\" app:layout_constraintStart_toStartOf=\"parent\" app:layout_constraintTop_toTopOf=\"parent\" /> |
<Button android:id=\"@id/btn\" android:text=\"Search\" android:onClick=\"search\" android:layout_width=\"wrap_content\" android:layout_height=\"wrap_content\" app:layout_constraintBottom_toBottomOf=\"parent\" app:layout_constraintEnd_toEndOf=\"parent\" app:layout_constraintStart_toStartOf=\"parent\" app:layout_constraintTop_toBottomOf=\"@id/editText\" /> |
Now, let's delve into how to work with the MainActivity file to create the backend of the app.
Below is a code snippet demonstrating the MainActivity file:
import android.content.Intent | import android.net.Uri | import android.os.Bundle |
import android.widget.Button | import android.widget.EditText | import androidx.appcompat.app.AppCompatActivity |
MainActivity : AppCompatActivity() {
lateinit var editText: EditText
override fun onCreate(savedInstanceState: Bundle?) {
.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
editText = findViewById(R.id.editText)
fun search() {
val url = editText.text.toString()
val urlIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(urlIntent)
After setting up the UI, proceed to creating the backend of the application by navigating to the MainActivity file. Use the findViewById() method to link the components (Button, TextView) from the XML file to the corresponding UI elements based on their assigned IDs.
File > new > Activity > Empty Activity.
Next, navigate to the activity_main2.xml file, which represents the UI of the project. Below is the code for the activity_main2.xml file. Comments are added inside the code for better understanding.
activity_main2.xml file
activity_main2.xml
MainActivity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); } public void homeScreen(View view) { Intent i = new Intent(getApplicationContext(), MainActivity.class); startActivity(i); } }
class MainActivity2 : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) } fun homeScreen() { val i = Intent(applicationContext, MainActivity::class.java) startActivity(i) } }