In order to pass any data from one Activity to another, there is a very important class provided by Android. It is Intent. There are two types of Intents: Implicit Intents and Explicit Intents. We can pass data from one Activity to another with the help of Explicit Intents.
First we create the layouts for first activity, i.e DataPassingActivity:
----------------------data_passing_activity.xml------------------------
---------------------DataPassingActivity.java----------------------
Now we create the layout for new activity where we catch the sent data from first activity:
---------------------data_receiving_activity.xml--------------------------
------------------------DataReceivingActivity.java---------------------
---------------------Declare the Activity names Manifest.xml file-------------------------
----------------------data_passing_activity.xml------------------------
<?xml version="1.0" encoding="utf-8"?>-------------------------------------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#795548"
android:padding="20dp" >
<TextView
android:id="@+id/textView1"
android:layout_marginBottom="5dp"
android:textSize="25sp"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Activity 1"/>
<EditText
android:id="@+id/enterTextET"
android:gravity="center"
android:textSize="25sp"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:id="@+id/sendBTN"
android:layout_width="match_parent"
android:layout_height="80dp"
android:textColor="#000000"
android:textSize="20sp"
android:background="#A1887F"
android:text="Click Me to send Text." />
</LinearLayout>
---------------------DataPassingActivity.java----------------------
package com.app.yourappname;------------------------------------------------------------------
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class DataPassingActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.data_passing_activity);
Button sendTextBTN;
final EditText sendTextET;
final Intent intent = new Intent();
intent.setClass(this, DataReceivingActivity.class);
sendTextET = findViewById(R.id.enterTextET);
sendTextBTN = findViewById(R.id.sendBTN);
sendTextBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent.putExtra("message", sendTextET.getText().toString());
startActivity(intent);
}
});
}
}
Now we create the layout for new activity where we catch the sent data from first activity:
---------------------data_receiving_activity.xml--------------------------
<?xml version="1.0" encoding="utf-8"?>---------------------------------------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#5D4037"
android:padding="20dp" >
<TextView
android:id="@+id/textView1"
android:layout_marginBottom="5dp"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#ffffff"
android:text="Activity 2"/>
<TextView
android:layout_marginTop="30dp"
android:textSize="25sp"
android:text="Received Text:"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_marginTop="20dp"
android:id="@+id/receivedTextTV"
android:background="#BCAAA4"
android:textSize="25sp"
android:textColor="#000000"
android:layout_width="fill_parent"
android:layout_height="80dp"/>
</LinearLayout>
------------------------DataReceivingActivity.java---------------------
package com.app.yourappname;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class DataReceivingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_receiving_activity);
String str = getIntent().getStringExtra("message");
TextView receivedMsgTV = findViewById(R.id.receivedTextTV);
receivedMsgTV.setText(str);
}
}
--------------------------------------------------------------------
---------------------Declare the Activity names Manifest.xml file-------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".DataPassingActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DataReceivingActivity"/>
</application>
</manifest>
-----------------------------------------------------------------
No comments:
Post a Comment