Activity与Fragment通信
原生方案:Bundle
BlankFragment1.java中
Bundle bundle = this.getArguments();
String string = bundle.getString("message");
Log.d(TAG, "onCreate: "+string);
MainActivity.java
public void onClick(View view) {
switch (view.getId()){ //根据不同id点击,显示不同Button
case R.id.btn: //此处为冒号
Bundle bundle = new Bundle();
bundle.putString("message","一条信息");
BlankFragment1 bf = new BlankFragment1();
bf.setArguments(bundle);
replaceFragment(bf);
//replaceFragment(new BlankFragment1());//创建一个函数
break;
case R.id.btn2:
replaceFragment(new ItemFragment());//又要new一个Fragment
}
}
运行效果,点击Button后
传入
fragment动态添加与管理总结
1.创建一个待处理的fragment
2.获取FragmentManager,一般通过getSupportFragmentManager()
3.开启一个事务transaction,一般调用fragmentManager的beginTransaction()
4.使用transaction进行fragment的替换
5.提交事务
//动态切换frahment
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager(); //获取默认管理类
FragmentTransaction transation = fragmentManager.beginTransaction();
transation.replace(R.id.framelayout,fragment);
//像activity一样有栈,添加到栈中,点击返回键,有复现
//transation.addToBackStack(null);
transation.commit();//有管理站
}
Java语言中类与类自己通信常用方案:接口
从Fragment里面发送消息给Activity
定义一个接口IFragmentCallback.java
package com.example.fragmentmanagers;
public interface IFragmentCallback {
void sendMsgToActivity(String msg);
//取
String getMsgFromActivity(String msg);
}
添加一个数据传递的按钮
修改对应的fragment_blank1.xml文件
其BlankFragment1.java文件修改如下
package com.example.fragmentmanagers;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class BlankFragment1 extends Fragment {
private static final String TAG = "BlankFragment1";
private View rootview;
public BlankFragment1() {
// Required empty public constructor
}
private IFragmentCallback fragmentCallback;
public void setFragmentCallback(IFragmentCallback callback){
fragmentCallback = callback;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
String string = bundle.getString("message");
Log.d(TAG, "onCreate: "+string);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if(rootview == null){
rootview = inflater.inflate(R.layout.fragment_blank1,container,false);
}
Button btn = rootview.findViewById(R.id.btn_3);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//fragmentCallback.sendMsgToActivity("Hello,I'm from Fragment");
String msg = fragmentCallback.getMsgFromActivity("null");
//Toast.makeText(BlankFragment1.this,msg, Toast.LENGTH_SHORT).show();//这样写不行,因为Fragment没有上下文
Toast.makeText(BlankFragment1.this.getContext(),msg,Toast.LENGTH_SHORT).show();
}
});
return rootview;
}
}
MainActivity.java
package com.example.fragmentmanagers;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ //点击事件的两种方式
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.btn);
button.setOnClickListener(this); //传入this,默认面向对象的原则,会自动触发onClick函数
Button button2 = findViewById(R.id.btn2);
button2.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){ //根据不同id点击,显示不同Button
case R.id.btn: //此处为冒号
Bundle bundle = new Bundle();
bundle.putString("message","一条信息");
BlankFragment1 bf = new BlankFragment1();
bf.setArguments(bundle);
bf.setFragmentCallback(new IFragmentCallback() {
@Override
public void sendMsgToActivity(String msg) {
//拿到消息
Toast.makeText(MainActivity.this,msg, Toast.LENGTH_SHORT).show();//注意是MainActivity.this
}
@Override
public String getMsgFromActivity(String msg) {
//return null;
return "Hello,I'm from Activity";
}
});
//创建一个类型对象,抽象的,不能被初始化
//IFragmentCallback Icall = new IFragmentCallback();
//匿名内部类
replaceFragment(bf);
//replaceFragment(new BlankFragment1());//创建一个函数
break;
case R.id.btn2:
replaceFragment(new ItemFragment());//又要new一个Fragment
}
}
//动态切换frahment
private void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager(); //获取默认管理类
FragmentTransaction transation = fragmentManager.beginTransaction();
transation.replace(R.id.framelayout,fragment);
//像activity一样有栈,添加到栈中,点击返回键,有复现
//transation.addToBackStack(null);
transation.commit();//有管理站
}
}
从Activity中取出来
观察者,发布订阅
fragment activity
发布-应允
面向对象思想