1、说明
在Activity中调用setArguments(Bundle bundle)进行传递,
在指定的Fragment中调用getArguments接收传递过来的Bundle
2、MainActivity.java
package com.example.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button add = findViewById(R.id.add);
add.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.add:
addFragmentToView(new FirstFragment());
break;
default:
break;
}
}
private void addFragmentToView(FirstFragment firstFragment) {
/**
* 通过bundle实现activity和fragment之间的信息传递
*/
Bundle bundle = new Bundle();
bundle.putString("message","恭喜你,通过bundle传递参数成功");
firstFragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.addToBackStack(null)
.add(R.id.fragment_layout,firstFragment)
.commit();
}
}
2、FirstFragment.java
package com.example.fragment;
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.TextView;
/**
* A simple {@link Fragment} subclass.
* create an instance of this fragment.
*/
public class FirstFragment extends Fragment {
private View mInflate;
private Button mBtn;
private TextView mText;
private String mMessage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mInflate = inflater.inflate(R.layout.fragment_first, container, false);
mBtn = mInflate.findViewById(R.id.first_button);
mText = mInflate.findViewById(R.id.first_text);
/**
* 通过getArguments()获取传递过来的Bundle
*/
mMessage = getArguments().getString("message");
mBtn.setOnClickListener(view -> {
mText.setText(mMessage);
});
return mInflate;
}
}
3、activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/add"
android:text="@string/add"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_layout"
android:background="@color/purple_200"/>
</LinearLayout>
4、fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:orientation="vertical"
android:background="#889933"
tools:context=".FirstFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/first_text"
android:textSize="30dp"
android:text="@string/message"/>
<Button
android:id="@+id/first_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/please_click" />
</LinearLayout>
</FrameLayout>