-
Fragment transfers data to activity -
Activity transfers data to Fragment -
Communication mode between fragments
1、 Fragment transfers data to activity
//Define Interface public interface DataTransferInterface { void onDataReceived(String data); } //Fragment Implementation public class MyFragment extends Fragment { private DataTransferInterface dataTransferInterface; @Override public void onAttach(@NonNull Context context) { super.onAttach(context); if (context instanceof DataTransferInterface) { dataTransferInterface = (DataTransferInterface) context; } else { throw new RuntimeException("Activity must implement DataTransferInterface"); } } public void sendData(String data) { dataTransferInterface.onDataReceived(data); } } //Activity Implementation Interface public class MainActivity extends AppCompatActivity implements DataTransferInterface { @Override public void onDataReceived(String data) { //Processing data transferred from Fragment } }
public class SharedViewModel extends ViewModel { private MutableLiveData<String> mMessage = new MutableLiveData<>(); public LiveData<String> getMessage() { return mMessage; } public void setMessage(String message) { mMessage.setValue(message); } }
fragment_share_data.xml
<?xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter some text" /> <Button android:id="@+id/button_share" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Share Data" /> </LinearLayout> ShareDataFragment.java
public class ShareDataFragment extends Fragment { private SharedViewModel viewModel; private EditText editText; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_share_data, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); viewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class); editText = view.findViewById(R.id.edit_text); view.findViewById(R.id.button_share).setOnClickListener(v -> { viewModel.setMessage(editText.getText().toString()); }); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { private TextView textView; private SharedViewModel viewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text_view); viewModel = new ViewModelProvider(this).get(SharedViewModel.class); viewModel.getMessage().observe(this, message -> { textView.setText(message); }); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .replace(R.id.fragment_container, new ShareDataFragment()) .commit(); } } }
dependencies { implementation 'org.greenrobot:eventbus:3.3.1' }
public class MessageEvent { public final String message; public MessageEvent(String message) { this.message = message; } }
fragment_share_data.xml
<?xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android " android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter some text" /> <Button android:id="@+id/button_share" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Share Data" /> </LinearLayout>
ShareDataFragment.java
public class ShareDataFragment extends Fragment { private EditText editText; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_share_data, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); editText = view.findViewById(R.id.edit_text); view.findViewById(R.id.button_share).setOnClickListener(v -> { String message = editText.getText().toString(); EventBus.getDefault().post(new Mess