activity与fragment的原生通信:bundle方法,请点这里看 大标题三
一 步骤
1 定义接口:写发消息和收消息的两个方法
2 Fragment.xml布局
3 Fragment.java
写一个属性是刚刚自定义的接口,写set方法
点击事件时,调接口的发消息和收消息方法
收到消息方显示吐司:Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
4 Main.java
new Fragment之后set自定义的接口,实现接口发消息和收消息的方法
收到消息方显示吐司:Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
二 代码
1 定义接口:写发消息和收消息的两个方法
IFragmentCallback.java
package com.example.myfragmentdong;
public interface IFragmentCallback {
//发消息给activity
void sendMsgToActivity(String msg);
//从activity取消息
String getMsgFromActivity();
}
2 Fragment.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".BlankFragment1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:textSize="50sp"
android:text="hello boy" />
<Button
android:id="@+id/button"
android:text="发信"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/button2"
android:text="收信"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
3 Fragment.java
package com.example.myfragmentdong;
import android.content.Context;
import android.net.Uri;
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.Toast;
public class BlankFragment2 extends Fragment {
private IFragmentCallback fragmentCallback;
private View rootview;
public void setFragmentCallback(IFragmentCallback fragmentCallback) {
this.fragmentCallback = fragmentCallback;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(rootview == null){
rootview = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
}
//fragment发消息(来自fragment)
Button btn = rootview.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragmentCallback.sendMsgToActivity("hello,我是fragment派来的消息");
}
});
//fragment收消息(来自activity)
Button btn2 = rootview.findViewById(R.id.button2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(BlankFragment2.this.getContext(),fragmentCallback.getMsgFromActivity(),Toast.LENGTH_LONG).show();
}
});
return rootview;
}
}
4 Main.java
我把全代码都贴过来
package com.example.myfragmentdong;
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{
private Button btn;
private Button btn2;
private Button btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
btn.setOnClickListener(this);
btn2 = findViewById(R.id.btn2);
btn2.setOnClickListener(this);
btn3 = findViewById(R.id.btn3);
btn3.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.btn:
//通信:activity把信息发给fragment
Bundle bundle = new Bundle();
bundle.putString("message","i love android");
BlankFragment1 fragment1 = new BlankFragment1();
fragment1.setArguments(bundle);
replacefragement(fragment1);
break;
case R.id.btn2:
BlankFragment2 fragment2 = new BlankFragment2();
//通过接口通信
fragment2.setFragmentCallback(new IFragmentCallback(){
@Override
public void sendMsgToActivity(String msg) {
Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
}
@Override
public String getMsgFromActivity() {
return "hello,我是来自activity的消息";
}
});
replacefragement(fragment2);
break;
case R.id.btn3:
replacefragement(new ItemFragment());
break;
}
}
//动态切换fragment
private void replacefragement(Fragment fragment) {
//专门管理fragment的
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
//用第2个替换第一个
transaction.replace(R.id.framelayout,fragment);
//添加栈
transaction.addToBackStack(null);
//提交事务
transaction.commit();
}
}
效果
fragment与activity的接口通信