Created
June 15, 2022 13:01
-
-
Save ankitchauhan20/21227b27d30a52de1c13e846854efe9f to your computer and use it in GitHub Desktop.
Pass Data between fragment to activity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout | |
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" | |
> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".GetFragmentDataActivity"> | |
<FrameLayout | |
android:id="@+id/frameLayout" | |
android:layout_width="match_parent" | |
android:layout_height="400dp" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintBottom_toTopOf="@id/activity" | |
/> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/activity" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Activity" | |
android:textSize="20dp" | |
android:textStyle="bold" | |
android:layout_marginTop="50dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintHorizontal_bias="0.021" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintVertical_bias="0.647" /> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/get_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="textview" | |
android:textSize="18sp" | |
app:layout_constraintTop_toBottomOf="@id/activity" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
/> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.oopstutorial; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import androidx.fragment.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Button; | |
import android.widget.EditText; | |
public class DataFragment extends Fragment { | |
EditText editText; | |
Button button; | |
public interface onSomeEventListener { | |
void someEvent(String s); | |
} | |
onSomeEventListener someEventListener; | |
@Override | |
public void onAttach(Activity activity) { | |
super.onAttach(activity); | |
try { | |
someEventListener = (onSomeEventListener) activity; | |
} catch (ClassCastException e) { | |
throw new ClassCastException(activity.toString() + " must implement onSomeEventListener"); | |
} | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View v = inflater.inflate(R.layout.fragment_data, null); | |
button = v.findViewById(R.id.sendBtn); | |
editText = v.findViewById(R.id.edit_text); | |
button.setOnClickListener(v1 -> { | |
String data = editText.getText().toString(); | |
someEventListener.someEvent(data); | |
}); | |
return v; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout 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" | |
android:background="#DFDFDF" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
tools:context=".DataFragment"> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/fragment" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Fragment" | |
android:layout_margin="10dp" | |
android:textStyle="bold" | |
android:textSize="20dp" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
/> | |
<androidx.appcompat.widget.AppCompatEditText | |
android:id="@+id/edit_text" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Text" | |
android:textSize="20sp" | |
android:layout_gravity="center" | |
android:layout_marginHorizontal="20dp" | |
android:paddingStart="5dp" | |
app:layout_constraintTop_toBottomOf="@id/fragment" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
android:layout_marginTop="50dp" | |
/> | |
<androidx.appcompat.widget.AppCompatButton | |
android:id="@+id/sendBtn" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:text="Send Data" | |
android:layout_marginTop="10dp" | |
android:gravity="center" | |
app:layout_constraintTop_toBottomOf="@id/edit_text" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
/> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.oopstutorial; | |
import android.os.Bundle; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.databinding.DataBindingUtil; | |
import androidx.fragment.app.FragmentManager; | |
import com.example.oopstutorial.databinding.ActivityGetFragmentDataBinding; | |
public class GetFragmentDataActivity extends AppCompatActivity implements DataFragment.onSomeEventListener { | |
ActivityGetFragmentDataBinding binding; | |
DataFragment dataFragment = new DataFragment(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
binding = DataBindingUtil.setContentView(this, R.layout.activity_get_fragment_data); | |
FragmentManager fragmentManager = getSupportFragmentManager(); | |
fragmentManager.beginTransaction() | |
.replace(R.id.frameLayout, dataFragment) | |
.commit(); | |
} | |
@Override | |
public void someEvent(String s) { | |
binding.getText.setText(s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment