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

Intentintent=newIntent(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 :
<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns: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"><TextViewandroid: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"/><Buttonandroid: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 :
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid: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"/><Buttonandroid: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 :
packagecom.example.putuguna.projecttesting;importandroid.content.Intent;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;publicclassMainActivityextendsAppCompatActivity{privateButtonmButtonStart;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//kita ambil ID yang ButtonmButtonStart=(Button)findViewById(R.id.btn_start);//We set the onClick of the button start//After that, we go to the next activity using intentmButtonStart.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){Intentintent=newIntent(MainActivity.this,SecondActivity.class);// means : From MainActivity.java will going to go to the SecondActivity.javastartActivity(intent);}});}}
Modify your SecondActivity.java, like the following code :
packagecom.example.putuguna.projecttesting;importandroid.os.Bundle;importandroid.support.annotation.Nullable;importandroid.support.v7.app.AppCompatActivity;importandroid.view.View;importandroid.widget.Button;/** * Created by putuguna on 17/06/16. */publicclassSecondActivityextendsAppCompatActivity{privateButtonmBtnBack;@OverrideprotectedvoidonCreate(@NullableBundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);//Ambil ID si Button BackmBtnBack=(Button)findViewById(R.id.btn_back);//Set on CLick si Button BackmBtnBack.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){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

Post a Comment

Pages

Copyright © 2019

kalaschnikow blog