Android PassData Between Activities
PassData Between Activities:
interactivity entails sharing of data between activities, whereby information is passed from one activity to the other.
This is usually accomplished by the use of Intent objects which provide facilities for data passing by key values.
This process enables you to pass information such as a string, integer or any other object between various modules within your application.
Example:
// activity_pass_data_between_activities_example.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".PassDataBetweenActivitiesExample"
android:gravity="center"
android:orientation="vertical"
android:background="@color/colorOrange">
<FrameLayout
android:id="@+id/pasFrame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/passlin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:layout_gravity="center"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fill Below Details"
android:textColor="@color/colorWhite"
android:textSize="37dp"
android:gravity="center"/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"
android:layout_marginTop="16dp"/>
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Enter your age"/>
<Button
android:id="@+id/submit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textColor="@color/colorWhite"
android:layout_gravity="center"
android:layout_marginTop="25dp"
android:background="@color/colorAccent"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
// activity_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context=".ActivityTwo"
android:gravity="center"
android:padding="16dp"
android:orientation="vertical"
android:background="@color/colorPrimaryDark">
<FrameLayout
android:id="@+id/pasFrame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/passlin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:layout_gravity="center"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the Second Activity"
android:textColor="@color/colorWhite"
android:textSize="30dp"
android:gravity="center"
android:layout_marginBottom="50dp"/>
<TextView
android:id="@+id/text_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="@color/colorWhite"
android:textSize="30dp"
android:gravity="center"/>
<TextView
android:id="@+id/text_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="@color/colorWhite"
android:textSize="30dp"
android:gravity="center"/>
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:textColor="@color/colorWhite"
android:gravity="center"
android:background="@color/colorOrange"
android:layout_marginTop="30dp"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
Output