Android Explicit Intent Example

Intents are used as a message passing mechanism within the application and between the applications.

An intent generically defines an “intention” to do some work. Intents encapsulate several concepts

You can use intents to invoke other applications from your application. You can use intents to invoke internal or external components from your application .

  Intents are used to perform the following tasks :-

  •    Start Activity to launch an Activity
  •    Broadcast Intent to send it to any interested Broadcast Receiver components. 
  •    Start Service to communicate with a background Service .intents ares always initiated by your application , An intent can be Explicit or implicit .


Explicit Intent: - 

In an explicit intent, we actually specify the activity that is required to respond to the intent. In other words, we explicitly designate the target component.

Project Structure



file : build.gradle
 
 apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.tutorialsbuzz.explicitintent"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.0.0-beta1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}



file: activity_main.xml
 
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
    android:orientation="vertical"
    tools:context="com.tutorialsbuzz.explicitintent.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/label_main_activity"
        android:textSize="30sp" />

    <Button
        android:id="@+id/callSecondActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/call_second_activity"
        android:textSize="30sp" />

</LinearLayout>



file : MainActivity.java
 
 package com.tutorialsbuzz.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 MainActivity extends AppCompatActivity {

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

        Button mButton = findViewById(R.id.callSecondActivity);

        mButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });

    }
}



file : second_activity.xml
 
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/label_second_activity"
        android:textSize="30sp" />

    <Button
        android:id="@+id/callMainActivity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/call_main_activity"
        android:textSize="30sp" />

</LinearLayout>



file : SecondActivity.java
 
 package com.tutorialsbuzz.explicitintent;

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

/**
 * Created by Pawan.Deshpande on 07-09-2018.
 */

public class SecondActivity extends AppCompatActivity {

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

        Button mButton = findViewById(R.id.callMainActivity);

        mButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SecondActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

    }
}



file : AndroidManifest.xml
 
 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tutorialsbuzz.explicitintent">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!-- Main Activity -->
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Second Activity -->
        <activity android:name=".SecondActivity" />

    </application>

</manifest>



No comments:

Post a Comment