Hey reader,
In this post, I will tell you about Explicit Intents in android. Implicit intent will be covered in the coming post.
In order to know about intents, you need to first know about ACTIVITY. Activity is a app component which can represent a single layout on the android screen at a time. It simply represents a screen in the android device.
Learn more about Activity from Google developer site.
There can be many activities in an application. When we use many activities (screens), we need to switch between these screens according to some conditions. This is where Intents come into the picture.
Intent is a communicator or messenger within your app. It can perform many operations like
1. Start a new activity from current activity
2. Start a new service.
3. Transfer data between activities in the app and many more.
Learn more about intents on Google developer site.
There are two types of intents,
1. Implicit intents: Used to perform general tasks like sending a message/broadcast, from other apps, from within your app.
2. Explicit Intents: Used to shift between activities within your own app.
We will be using Explicit intent to start a new activity in this tutorial.
Follow the steps:
- Open the 'Hello world' Application that you created during the previous tutorial '3. Creating a 'HELLO WORLD' app [Android development]'.
- 'Create a New activity' by right clicking on the 'java' folder in the 'project pane' on the left side of your android studio screen. Give the name as 'newactivity'.
- Alter the MainActivity.java as given below,
- Alter the activity_main.xml file to:
- MainActivity's layout includes a TextView which prints 'Activity 1' and a Button which shows 'Click me', on click of which we should navigate to second activity.
- Type the following into newactivity.java:
- Type the following in activity_newactivity.xml:
This Code includes a component called 'Toast'. Learn more about toast messages in the coming tutorials.
Video Tutorial for this post at 'Codeshuffle YouTube'
Cheers!!
Share your views about this article!