September 20, 2016

Android Intent - The man in-between



What is an Intent you may ask.

According Android Developers, an intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components,  and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background service.

It could also be referred to as a messaging object you can use to request an action from another app component.

Basically there are two types of intent. Explicit intent and implicit intent. Whereas explicit intent specifies the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, start a new activity in response to a user action or start a service to download a file in the background. Implicit intent on the other hand do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.


With that said, lets see how we can use intent in an android application.

Go to File in your IDE, click and choose New to create new application. (I assume you know how to create a new application so I will not proceed further on that). Give your application a name of your choice. For the benefit of this tutorial, am naming my application as ExplicitIntent and the launcher activity as  ActivityA and click finish.

Without wasting time, lets create another activity. This time I give it the name ActivityB to distinguish it from the first. ActivityB therefore happens to be a sub-activity of ActivityA. See snapshot below

STEP 1

STEP 2
With these two activities in place, lets go ahead and code.

First of all, lets set the XML files in order. In your activity_a.xml or whatever name you'd used to represent the first activity, here is the XML style or components on the file.

Activity A XML file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="technology.airwaves.com.explicitintent.ActivityA">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:text="Launch Activity B"
        android:onClick="startActivityB"
        android:id="@+id/btn1"/>
</RelativeLayout>

In XML file above, we're using xml's onclick event handler to call the method startActivityB when the button is pressed.

Activity B XML file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="technology.airwaves.com.explicitintent.ActivityB">
    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="center"
    android:id="@+id/imageView1"
    android:src="@drawable/coffee_cup"
    />
</RelativeLayout>



Here comes the code for the first activity (ActivityA).
package technology.airwaves.com.explicitintent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class ActivityA extends AppCompatActivity {
Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);

        btn = (Button)findViewById(R.id.btn1);


    }

    public void startActivityB(View v){
        Intent intent = new Intent(this,ActivityB.class);
        startActivity(intent);
    }
}


And the second activity (ActivityB)
package technology.airwaves.com.explicitintent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class ActivityB extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);

    }
}

As simple and straight forward as that. When the button is clicked in activity A, the method startActivityB() is called. Inside this method, we have defined it to start an explicit intent and call ActivityB who happens to be our target or interest. Intent takes two parameters, the application context and the name of the class to be called with the .class extension.

If you have any question or doubt, leave a comment below and I will be glad to answer you. If you like this, you can comment as well and recommend it to your friends. Thank you.







0 comments:

Post a Comment