Android Explicit Intent (Move from One Activity to Another)

Android Explicit Intent: Here, we will learn how to write an Android example to implement explicit intent and how to use explicit intent and to move from one activity to another and vice-versa. By Shamikh Faraz Last updated : June 08, 2023

Android Explicit Intent Example: Move from One Activity to Another and Vice-Versa

Using Explicit Intent, we move from one activity to another activity. Through this intent, information can also be passed from one activity to another.

Code Files

Here are the code files of this Android example to implement explicit intent.

XML file: (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.faraz.explicit_intent.MainActivity">

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/TextView01"
        android:layout_marginTop="209dp"
        android:onClick="onClick"
        android:text="Go To Other Activity"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="OnClick" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Button01"
        android:layout_alignParentTop="true"
        android:layout_marginTop="44dp"
        android:minHeight="60dip"
        android:text="This is First Activity"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

XML file: (activity_other.xml)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.faraz.explicit_intent.ActivityTwo">

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/TextView01"
        android:layout_marginBottom="212dp"
        android:onClick="onClick"
        android:text="Go to Home Activity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="OnClick" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Button02"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="71dp"
        android:layout_marginTop="101dp"
        android:minHeight="60dip"
        android:text="This is Second Activity"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Java file: (MainActivity.java)

package com.example.faraz.explicit_intent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
  Button button1;
  // This code is called when the activity creates. /  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button1 = (Button) findViewById(R.id.Button01);

    button1.setOnClickListener(new OnClickListener() {
      public void onClick(View view) {
        // Intent is used to move from home activity to other activity  
        Intent intent = new Intent(getApplicationContext(), OtherActivity.class);
        startActivity(intent);
      }
    });
  }
}

Java file: (OtherActivity.java)

package com.example.faraz.explicit_intent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class OtherActivity extends Activity {
  Button button2;

  // This code is called when the activity creates. /  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_other);
    Button button2 = (Button) findViewById(R.id.Button02);

    button2.setOnClickListener(new View.OnClickListener() {

      public void onClick(View view) {
        // Intent is used to move from other activity to home activity  
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        startActivity(intent);
      }
    });
  }
}

Output

After clicking on 'Go To Other Activity' button, we will move to second activity and vice-versa

explicit intent code in android


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.