Leave a rating/review
To navigate to the about acitvity, you’ll be working with something called an Intent. An Intent is just an object that that is used to communicate between components in Android. It is commonly used to communicate between activities. For example, it can be used to start another activity and it can also be used to send data between activities.
You’ll create an Intent to start the AboutActivity.
Let’s do that now.
Open up the MainActivity.kt file.
Let’s create a new method that would contain the code to start up the about activity.
Add the following method below the onCreate method of the activity.
private fun navigateToAboutPage() {
}
Next, let’s create an instance of an intent:
val intent = Intent(this, AboutActivity::class.java)
We create an intent and pass in the MainActivity as its context.
For starters, context here just refers to the environment where this intent is called.
this as used in this case refers to the current environment of execution.
The main activity would be the value of this since this code is declared inside the main activity.
Next is the class that is going to packaged with the intent.
For that we pass in the AboutActivity‘s java class instance.
We use the java version because that’s what the Intent class needs.
Now that we’re done creating the instance of the intent, we need to answer the question: What’s the Intention? The answer is simple: “The intention is to start an activity.” Alright, enter the following code:
startActivity(intent)
We call the startActivity method and pass in the intent. This method starts an instance of the AboutActivity that is specified in the intent.
That’s it for this method. It’s time to use it. We need to call it whenever the info button is clicked. For this, enter the following code below the listener for the start over button:
binding.infoButton?.setOnClickListener {
navigateToAboutPage()
}
The code is pretty straightforward. Let’s try it out. Go ahead and run the app. Then click on the info button. Cool!!! It starts up the about activity as expected.
But if you notice, it displays the portrait version of the layout file and that’s why you can’t see the textviews and button. Those views were added only in the landscape variation. Remember, we set only the main activity to be landscape in the manifest file. You’ll set the about activity to landspace shorthly.
Now, android apps always have a back button up in the action bar beside the title But you can see this screen doesnt have it. Let’s add that now. Head over to the Android manifest file. Then add the following property to the about activity node:
android:parentActivityName=".MainActivity"
With this, the about activity is now a child of the parent activity in this case the main acitiviy. So whenever we start the about activity, it is placed on top of the main activity in the navigation stack. And it also has a back button to pop it off the stack and go back to the parent activity.
Next, add in the following code to the about activity to set it to landscape mode:
android:screenOrientation="landscape"
Run your app to see it in action. Click on the info button. You now have the back button and the views are now displayed. Go ahead and Click on the back button. And it takes you back to the main activity.
Click on the info button once more. You can see the title of this scareen just says bullseye. Let’s change that to about bullseye.
Head over to strings.xml You can see the string resource for the page title which was added to the starter project. Let’s use that now in the activity.
Head over the AboutActivity.kt file.
And set the title of the activity like so:
title = getString(R.string.about_page_title)
Then restart this activity to see the update. And the title is updated to about bullseye.
Finally, let’s make the back button in the UI functional. You will make it navigate back to the previous page.
Since, you’ve been working with viewBinding in this course, update the activity to use it like so:
private lateinit var binding: ActivityAboutBinding
override fun onCreate(savedInstanceState: Bundle?) {
...
binding = ActivityAboutBinding.inflate(layoutInflater)
setContentView(binding.root)
...
Then add in the code for the back button’s click listener:
binding.backButton?.setOnClickListener {
finish()
}
In here, you call the finish method.
This method signifies that you’re done with this acitivty and it closes it and navigates back
to the activity that started it.
And always remember to put the question mark before you call the click listener because we have two layout files and the
back button is only present in one which is the landscape variation.
With this, it calls setOnClickListener only when there is a back button present.
Okay. Run your app once again. Go to the about page. Then tap the back button. Cool!!! The app navigates back to the main activity when the back button is tapped.