Android PassData Between Fragment
PassData Between Fragment:
The communication of data between the fragments is a routine activity when developing an android application, and it’s a process intended for the transfer of knowledge from one fragment to another.
Example:
// activity_pass_data_fragment_example.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".PassDataFragmentExample">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
// fragment_fragment_first.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=".FragmentFirst"
android:padding="16dp"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your Text"
/>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:textColor="@color/colorWhite"
android:background="@color/colorPrimary"
android:layout_marginTop="50dp"
/>
</LinearLayout>
// fragment_fragment_second.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=".FragmentSecond"
android:background="#00FFFF"
android:gravity="center"
android:padding="16dp"
android:orientation="vertical">
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="55sp"
android:textColor="@color/colorWhite"
android:layout_marginBottom="20dp"
/>
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:textSize="20dp"
android:textAllCaps="false"
android:textColor="@color/colorWhite"
android:background="@color/colorOrange"
/>
</LinearLayout>
Output