FirstActivity.java
-----------------------
package com.sam.web.oneactivityintent;
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.ImageView;
import android.widget.TextView;
public class FirstActivity extends AppCompatActivity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.buttonId);
textView=(TextView) findViewById(R.id.textViewId);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
startActivityForResult(intent,1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1){
String value=data.getStringExtra("key");
textView.setText(value);
}
}
}
----------------------------============================================
Activity_main.xml
----------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
tools:context="com.sam.web.oneactivityintent.FirstActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonId"
android:text="@string/button1_text"
android:textSize="20sp"
android:textStyle="bold"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_marginBottom="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textViewId"
android:textSize="20sp"
android:textStyle="bold"
/>
</LinearLayout>
============================================================
string.xml
------------
<resources>
<string name="app_name">OneActivityIntent</string>
<string name="button1_text">Go to the Second Activity</string>
<string name="button2_text">Go to the first Activity</string>
</resources>
============================================================================
SecondActivity.java
---------------------
package com.sam.web.oneactivityintent;
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 SecondActivity extends AppCompatActivity {
private Button button;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button=(Button) findViewById(R.id.button2Id);
editText=(EditText) findViewById(R.id.editTextId);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value=editText.getText().toString();
Intent intent=new Intent(SecondActivity.this,FirstActivity.class);
intent.putExtra("key",value);
setResult(1,intent);
finish();
}
});
}
}
================================================================================
Activity_Second.xml
--------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
tools:context="com.sam.web.oneactivityintent.SecondActivity">
<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="@+id/editTextId"
android:textSize="20sp"
android:layout_marginBottom="20dp"
android:text="Enter your name"
/>
<Button
android:id="@+id/button2Id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/button2_text"
style="@style/Widget.AppCompat.Button.Colored"
/>
</LinearLayout>