Intent adalah object yang digunakan oleh Activity agar dapat berkomunikasi dengan Activity yang lain untuk suatu tindakan tertentu yang saling berhubungan. Dalam tutorial ini membahas tentang explicit intent yang dimana satu activity mengirim suatu object atau variable untuk memberikan tindakan pada suatu activity yang dituju.
Untuk mengetahui bagaimana membuat intent pada aplikasi android kita langsung saja membuat aplikasi seperti dibawah ini.
- Create New Android Project: File->New Android Project
- Modifikasi activity_main.xml seperti coding dibawah ini:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?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="cezas.com.androidintent.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/etNama" android:hint="Masukkan Nama"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/etAlamat" android:hint="Alamat" android:layout_below="@+id/etNama"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnPindah" android:layout_below="@+id/etAlamat" android:text="Pindah Activity"/> </RelativeLayout> |
3. Modifikasi code program pada MainActivity.java seperti dibawah ini:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package cezas.com.androidintent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { EditText etNama,etAlamat; Button btnPindah; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etNama=(EditText)findViewById(R.id.etNama); etAlamat=(EditText)findViewById(R.id.etAlamat); btnPindah=(Button)findViewById(R.id.btnPindah); btnPindah.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String nama=etNama.getText().toString(); String alamat=etAlamat.getText().toString(); System.out.println("Nama:"+nama); System.out.println("Alamat:"+alamat); Intent myIntent=new Intent(MainActivity.this,ActivityKedua.class); myIntent.putExtra("xNama",nama); myIntent.putExtra("xAlamat",alamat); startActivity(myIntent); } }); } } |
4. Membuat Activity baru dengan melakukan klik kanan pada package seperti pada gambar
new->activity->empty activity
5. Kemudian Buatlah dua buah TextView pada activity_activity_kedua.xml seperti kode program dibawah ini
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?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="cezas.com.androidintent.ActivityKedua"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvNama" android:text="Get Nama"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvAlamat" android:text="Get Alamat" android:layout_below="@+id/tvNama"/> </RelativeLayout> |
6. Setelah memodifikasi layout tersebut, modifikasi class ActivityKedua.java seperti dibawah ini:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package cezas.com.androidintent; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class ActivityKedua extends AppCompatActivity { TextView tvNama,tvAlamat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity_kedua); tvNama=(TextView)findViewById(R.id.tvNama); tvAlamat=(TextView)findViewById(R.id.tvAlamat); Intent myIntent=getIntent(); String ambilNama=myIntent.getStringExtra("xNama"); String ambilAlamat=myIntent.getStringExtra("xAlamat"); tvNama.setText(ambilNama); tvAlamat.setText(ambilAlamat); } } |
7. Setelah semua kode program telah dibuat dan tidak ada yang error silahkan jalankan aplikasi nya
semoga bermanfaat ^_^