Introducing Intent and different types of Intent
So far we have only dealt with single activity apps but now we are going to build apps with multiple activities and that’s where intent comes into play.
What is an Intent?
The official docs define intent as “An Intent
is a messaging object you can use to request an action from another app component.”. Essentially it means “an action” or intent to do stuff. You can use intents to launch another activity, to broadcast intent to background receivers (we will cover these later) and to communicate with background services.
Types of Intents
There are two types of intents.
- Explicit Intent
As the name suggests, explicit intent means you specify the component to start by name.
- Implicit Intent
Implicit intent means that you do not specify what component to use but just specify the action.
How to declare an Intent
The code below declares an intent to open another activity
Intent intent = new Intent(this, Target.class);
As you can see since it specifies the component to start (Target.class)it is an explicit intent to start another activity.
this refers to current activity.
Target is class name or activity name that you want to run or start.
Once declared, you can pass the intent to android as shown below.
startActivity(intent);
Declare an Implicit Intent
The example below will dial the number stored in number variable.
Intent callintent = new Intent (Intent.ACTION_DIAL, number);
Example
Let’s now build an app that launches another activity. You can continue with previous app we built or start a new blank empty project.
Add a simple text and button to your app as shown below.
Add a new Activity
There are many ways to add another activity to your app. Simplest is to just right click on your java folder in app structure and go to NEW> Activity>Empty Activity as shown below
This will open up a wizard similar to below.
Change the names if you wish to or just leave them as they are and now you should have two activities in your project structure.
Add a simple text to new activity
Declare and link your button in mainactivity.
Write an onclicklistener in mainactivity.
public class MainActivity extends AppCompatActivity { Button launch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); launch = (Button) findViewById(R.id.button); launch.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click } }); } }
Now let’s declare and start our intent.
public class MainActivity extends AppCompatActivity { Button launch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); launch = (Button) findViewById(R.id.button); launch.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click Intent launchActivity = new Intent(MainActivity.this, Main2Activity.class); startActivity(launchActivity); } }); } }
That’s it.
Let’s test our app.
Voila!
Now there is lots more to intents and we will cover those later.
Let me know any concerns via comments below.
Source and more reading
https://developer.android.com/guide/components/intents-filters.html
https://developer.android.com/training/basics/firstapp/starting-activity.html