目录
Activity 传递参数到Fragment,使用Bundle
Fragment 传递参数到 Activity 使用回调方法
前言
现在开发的很多项目都会使用Fragment,使用Fragment就是涉及到与Activity通信问题。
1.Activity与Fragment通信
Activity 传递参数到Fragment ,使用Bundle
Activity 传递参数 关键代码
//参数
Bundle bundle = new Bundle();
bundle.putString("name", "hello world");
//实例化fragment
AFragment aFragment = new AFragment();
aFragment.setArguments(bundle);
//添加fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container_ll_id, aFragment);
fragmentTransaction.commit();
Fragment 获取参数 关键代码
//获取参数
Bundle bundle = getArguments();
if (bundle != null) {
param1 = bundle.getString("name");
}
Toast.makeText(getContext(), param1, Toast.LENGTH_LONG).show();
示例代码如下
Activity 部分
package com.github.xiaohu409.androidutildemo.mvc.controller;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.github.xiaohu409.androidutildemo.R;
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initUI();
}
private void initUI() {
Button linkBtn = findViewById(R.id.link_btn_id);
linkBtn.setOnClickListener((View v) -> {
//参数
Bundle bundle = new Bundle();
bundle.putString("name", "hello world");
//实例化fragment
AFragment aFragment = new AFragment();
aFragment.setArguments(bundle);
//添加fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container_ll_id, aFragment);
fragmentTransaction.commit();
}
);
}
}
Fragment 部分
package com.github.xiaohu409.androidutildemo.mvc.controller;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import com.github.xiaohu409.androidutildemo.R;
/**
* AFragment
*/
public class AFragment extends Fragment {
private String param1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//获取参数
Bundle bundle = getArguments();
if (bundle != null) {
param1 = bundle.getString("name");
}
Toast.makeText(getContext(), param1, Toast.LENGTH_LONG).show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a, container, false);
}
}
Fragment 传递参数到 Activity 使用回调方法
操作步骤如下
1. 在Fragment里定义回调方法
/**
* 回调方法
*/
public interface Callback {
void callback(String name);
}
2.在Fragment实例化回调方法
//回调引用
private Callback callback;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
//实例化引用
if (context instanceof Callback) {
callback = (Callback) context;
}
}
3.在Fragment调用回调方法
getView().findViewById(R.id.btn_id).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用回调方法
if (callback != null) {
callback.callback("fragment 回传 activity");
}
}
});
4.在Activity里实现回调方法
/**
* 实现回调方法
* @param name
*/
@Override
public void callback(String name) {
Toast.makeText(getBaseContext(), name, Toast.LENGTH_LONG).show();
}
示例代码如下
Fragment 部分
package com.github.xiaohu409.androidutildemo.mvc.controller;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.github.xiaohu409.androidutildemo.R;
/**
* AFragment
*/
public class AFragment extends Fragment {
private String param1;
//回调引用
private Callback callback;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
//实例化引用
if (context instanceof Callback) {
callback = (Callback) context;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//获取参数
Bundle bundle = getArguments();
if (bundle != null) {
param1 = bundle.getString("name");
}
Toast.makeText(getContext(), param1, Toast.LENGTH_LONG).show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getView().findViewById(R.id.btn_id).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用回调方法
if (callback != null) {
callback.callback("fragment 回传 activity");
}
}
});
}
/**
* 回调方法
*/
public interface Callback {
void callback(String name);
}
}
Activity 部分
package com.github.xiaohu409.androidutildemo.mvc.controller;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.github.xiaohu409.androidutildemo.R;
public class TestActivity extends AppCompatActivity implements AFragment.Callback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initUI();
}
private void initUI() {
Button linkBtn = findViewById(R.id.link_btn_id);
linkBtn.setOnClickListener((View v) -> {
//参数
Bundle bundle = new Bundle();
bundle.putString("name", "hello world");
//实例化fragment
AFragment aFragment = new AFragment();
aFragment.setArguments(bundle);
//添加fragment
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container_ll_id, aFragment);
fragmentTransaction.commit();
}
);
}
/**
* 实现回调方法
* @param name
*/
@Override
public void callback(String name) {
Toast.makeText(getBaseContext(), name, Toast.LENGTH_LONG).show();
}
}