A Good Approach To Write Intent For Starting Activity

Anik Dey
3 min readNov 17, 2019

In our day to day Android Application Development we need to start activity from another activity or fragment. In todays article we will discuss about a probable good way to start activity or how should you write your intents for starting an activity. Let us assume that we want to start SecondActivity from FirstActivity on a button click & SecondActivity needs two values to be passed to it. Here is how the code will look like. activity_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
tools:context=".FirstActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Activity"
/>
</LinearLayout>

And your FirstActivity.kt

class FirstActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_first)


button.setOnClickListener {
var intent = Intent(this, SecondActivity::class.java)
intent.putExtra(SecondActivity.EXTRA_ONE, "Value One")
intent.putExtra(SecondActivity.EXTRA_TWO, "Value Two")
startActivity(intent)
}

}
}

And your SecondActivity.kt

class SecondActivity : AppCompatActivity() {

companion object {
const val EXTRA_ONE = "extra_one"
const val EXTRA_TWO = "extra_two"
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)

var firstValue = intent.getStringExtra(EXTRA_ONE)
var secondValue = intent.getStringArrayExtra(EXTRA_TWO)

}
}

Now let us focus on this piece of code in the FirstActivity.kt

button.setOnClickListener {
var intent = Intent(this, SecondActivity::class.java)
intent.putExtra(SecondActivity.EXTRA_ONE, "Value One")
intent.putExtra(SecondActivity.EXTRA_TWO, "Value Two")
startActivity(intent)
}

This approach of starting activity has some problems. First of all think about a situation that your SecondActivity needs another value to be passed. That’s all right you can add another line like this.

intent.putExtra(SecondActivity.EXTRA_THREE, "Value Three")

What about if you have written code to start SecondActivity from many other place? You need to add this line in every single piece of code from where did you start SecondActivity. What if you missed one by chance. You are not going to have any compile time error. But you app can crash at runtime.

Think about a scenario where you need to perform some modification on the data before it is being passed to your SecondActivity. You will be required to introduce that change in every single place where you started SecondActivity and again there is a high chance that you may miss.

Another problem is that if you make any change in the names of the extra you declared in SecondActivity you must have to change it in the FirstActivity also. For example if you change

const val EXTRA_ONE = "extra_one"
to
const val EXTRA_FIRST_NAME = "first_name"

You will be required to change it in the FirstActivity like this

intent.putExtra(SecondActivity.EXTRA_ONE, "Value One")
to
intent.putExtra(SecondActivity.EXTRA_FIRST_NAME, "Value One")

We can solve this problems by writing a method that will return an Intent from SecondActivity like this and then start activity from FirstActivity. Change your SecondActivity like this

companion object {
private const val EXTRA_ONE = "extra_one"
private const val EXTRA_TWO = "extra_two"

fun newIntent(context: Context, firstValue: String, secondValue: String): Intent {
var intent = Intent(context, SecondActivity::class.java)
intent.putExtra(EXTRA_ONE, firstValue)
intent.putExtra(EXTRA_TWO, secondValue)
return intent
}

}

And in your FirstActivity start the activity like this.

button.setOnClickListener {
var intent = SecondActivity.newIntent(this, "Value One", "Value two")
startActivity(intent)
}

Now you can see that FirstActivity knows nothing how the data is being passed to SecondActivity. What are the extras or did you perform any modification on the data or not. If you change the names of the extras it doesn’t affects the FirstActiviy.

Well if you want to pass a new value just introduce a new argument in the newIntent() method. As you used this method to return the intent for starting SecondAcvity you don’t need to worry much about where you used this method to start SecondActivity cause if you try to run the app you are going to have compile time error for that missing argument. So now you know that what to do with that. You can handle it as per your requirement.

--

--