0% found this document useful (0 votes)
25 views

Android Explicit Intent Example

This document provides an example of using explicit intents in Android to move between activities. It includes XML layout files for two activities, along with Java code files for the two activities that use intents to start each other on button clicks, allowing navigation between the activities.

Uploaded by

Abdullah Munir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Android Explicit Intent Example

This document provides an example of using explicit intents in Android to move between activities. It includes XML layout files for two activities, along with Java code files for the two activities that use intents to start each other on button clicks, allowing navigation between the activities.

Uploaded by

Abdullah Munir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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);
}
});
}
}

You might also like