Buat 2 Class Java dan 2 Class Layout
Class Layout untuk Pemanggil
Class Layout untuk Penerima
Class Java untuk Pemanggil
Class Java untuk Penerima
Class Layout untuk Pemanggil
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Pemanggil" /> <TextView android:text="Parameter Pemanggil" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:layout_height="wrap_content" android:id="@+id/editTextParameterPemanggil" android:layout_width="match_parent"> <requestFocus></requestFocus> </EditText> <TextView android:text="Parameter Penerima" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:layout_height="wrap_content" android:id="@+id/editTextParameterPenerima" android:layout_width="match_parent"></EditText> <Button android:text="Buka Layout Penerima" android:id="@+id/btnPemanggil" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> </LinearLayout>
Class Layout untuk Penerima
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Penerima" /> <TextView android:text="Parameter Pemanggil" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:layout_height="wrap_content" android:id="@+id/editTextParameterPemanggil" android:layout_width="match_parent"> <requestFocus></requestFocus> </EditText> <TextView android:text="Parameter Penerima" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <EditText android:layout_height="wrap_content" android:id="@+id/editTextParameterPenerima" android:layout_width="match_parent"></EditText> <Button android:text="Buka Layout Pemanggil" android:id="@+id/btnPenerima" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> </LinearLayout>
Class Java untuk Pemanggil
import android.app.Activity; 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 TheIntentActivity1 extends Activity { private static final int REQUEST_CODE = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_the_intent1); Button btnPemanggil = (Button) findViewById(R.id.btnPemanggil); btnPemanggil.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // hanya manggil saja, tanpa passing parameter// Intent intent = new Intent(TheIntentActivity1.this, TheIntentActivity2.class);// startActivity(intent); EditText textPemanggil = (EditText) findViewById(R.id.editTextParameterPemanggil); String dataactivity_1 = textPemanggil.getText().toString(); Intent intent = new Intent(TheIntentActivity1.this, TheIntentActivity2.class); intent.putExtra("value1", dataactivity_1 ); startActivityForResult(intent, REQUEST_CODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) { if (data.hasExtra("return_value1")) { EditText text2 = (EditText) findViewById(R.id.editTextParameterPenerima); text2.setText(data.getExtras().getString("return_value1")); } } } }
Class Java untuk Penerima
import android.app.Activity; 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 TheIntentActivity2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_the_intent2); Button btnPenerima = (Button) findViewById(R.id.btnPenerima); Bundle extras = getIntent().getExtras(); if (extras == null) { return; } String value1 = extras.getString("value1"); if (value1 != null ) { EditText textPenerima = (EditText) findViewById(R.id.editTextParameterPemanggil); textPenerima.setText(value1); } btnPenerima.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // hanya manggil saja, tanpa passing parameter// Intent intent = new Intent(TheIntentActivity2.this, TheIntentActivity1.class);// startActivity(intent); finish(); } }); } @Override public void finish() { Intent data = new Intent(); EditText textPenerima = (EditText) findViewById(R.id.editTextParameterPenerima); String dataactivity_2 = textPenerima.getText().toString(); data.putExtra("return_value1", dataactivity_2); setResult(RESULT_OK, data); super.finish(); } }
Komentar
Posting Komentar