Monday, 5 December 2016

Android How To Move To The Other Activity Using Intent

In this tutorial, I will explain how to move from one activity to other activity by using Intent.


Intent have abstract description from the operation. Intent can used with startAtivity for calling new activity, broadcastIntent from send broadcastReceiver and startService or bindService for communicate with background service.

Implementation


Intent intent = new Intent(ActivitySaatIni.this, ActivityYangDituju.class);
startActivity(intent);

Prepared 2 xml layout :
  1. activity_main.xml
  2. acitivity_second.xml
Preapre 2 Java classes :
  1. MainActivity.java
  2. SecondActivity.java

The xml code for activity_main.xml, look likes this :

<?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="com.example.putuguna.projecttesting.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textAllCaps="true"
        android:text="Hello World! I am in the First Activity" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="match_parent"
        android:text="Start"
        android:layout_height="wrap_content" />

</RelativeLayout>

For activity_second.xml, look likes this :

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

    <TextView
        android:layout_weight="1"
        android:id="@+id/second_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="HALLO YOU ARE IN THE SECOND ACTIVITY"
        android:gravity="center"/>

    <Button
        android:id="@+id/btn_back"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Back"/>

</LinearLayout>

After we created the layout, now modify our java classes. Create an onClick for the button that we used to calling new activity. The code of MainActivity.java, like the following :

package com.example.putuguna.projecttesting;

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 {

    private Button mButtonStart;

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

        //kita ambil ID yang Button
        mButtonStart = (Button) findViewById(R.id.btn_start);


        //We set the onClick of the button start
        //After that, we go to the next activity using intent

        mButtonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class); // means : From MainActivity.java will going to go to the SecondActivity.java
                startActivity(intent);
            }
        });
    }
}

Modify your SecondActivity.java, like the following code :

package com.example.putuguna.projecttesting;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

/**
 * Created by putuguna on 17/06/16.
 */
public class SecondActivity extends AppCompatActivity{

    private Button mBtnBack;

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

        //Ambil ID si Button Back
        mBtnBack = (Button) findViewById(R.id.btn_back);

        //Set on CLick si Button Back
        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
                /*If you need to call the previous activity, just use onBackPressed()*/
            }
        });
    }
}

To back to the previous page by using a button (not button back on your phone) just write onBackPressed() inside your onClick.
onBackPressed() is a default method that used to back to the previous Activity 
Ok, that's all


EmoticonEmoticon