九 fragment与activity通信-爱代码爱编程
Activity->Fragment
在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法
package com.example.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
private RadioGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
group = (RadioGroup) findViewById(R.id.RadioGroup);
group.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.first:
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
break;
case R.id.second: {
MyFragment2 fragment2 = new MyFragment2();
FragmentManager fragmentManager = getFragmentManager();
//获取开启事务的对象
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
//第一个参数:要把fragment加载到哪个布局文件 第二个参数为fragment对象
beginTransaction.add(R.id.fragment, fragment2);
//通过物理返回可以回退到上个状态
beginTransaction.addToBackStack(null);
//事务提交
beginTransaction.commit();
break;
}
case R.id.third: {
break;
}
case R.id.fourth: {
Intent intent2=new Intent(this,MainActivity4.class);
startActivity(intent2);
break;
}
}
}
}
package com.example.fragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import javax.microedition.khronos.egl.EGLDisplay;
public class MainActivity4 extends Activity {
private EditText editText;
private Button send;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main4);
editText = findViewById(R.id.editText);
send = findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText.getText().toString();
MyFragment5 myFragment5 = new MyFragment5();
Bundle bundle = new Bundle();
//传递字符串类型的字,key=name
bundle.putString("name",text);
myFragment5.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
//动态加载时设置fragment的tag
beginTransaction.add(R.id.layout,myFragment5,"fragment");
beginTransaction.commit();
Toast.makeText(MainActivity4.this,"向Fragment发送数据"+text,Toast.LENGTH_SHORT).show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"
android:id="@+id/send"/>
</LinearLayout>
package com.example.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import org.w3c.dom.Text;
public class MyFragment5 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2,container,false);
TextView tv = view.findViewById(R.id.text);
//接收端获取数据包
String text = getArguments().get("name")+"";
tv.setText(text);
Toast.makeText(getActivity(),"已成功接收到"+text,Toast.LENGTH_SHORT).show();
return view;
}
}
Fragment->Activity
在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口
动态加载Fragment
package com.example.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
private RadioGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
group = (RadioGroup) findViewById(R.id.RadioGroup);
group.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.first:
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
break;
case R.id.second: {
MyFragment2 fragment2 = new MyFragment2();
FragmentManager fragmentManager = getFragmentManager();
//获取开启事务的对象
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
//第一个参数:要把fragment加载到哪个布局文件 第二个参数为fragment对象
beginTransaction.add(R.id.fragment, fragment2);
//通过物理返回可以回退到上个状态
beginTransaction.addToBackStack(null);
//事务提交
beginTransaction.commit();
break;
}
case R.id.third: {
break;
}
case R.id.fourth: {
Intent intent2=new Intent(this,MainActivity4.class);
startActivity(intent2);
break;
}
}
}
}
package com.example.fragment;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import javax.microedition.khronos.egl.EGLDisplay;
public class MainActivity4 extends Activity implements MyFragment5.MyListener {
private EditText editText;
private Button send;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main4);
editText = findViewById(R.id.editText);
send = findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText.getText().toString();
MyFragment5 myFragment5 = new MyFragment5();
Bundle bundle = new Bundle();
//传递字符串类型的字,key=name
bundle.putString("name",text);
myFragment5.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
//动态加载时设置fragment的tag
beginTransaction.add(R.id.layout,myFragment5,"fragment");
beginTransaction.commit();
Toast.makeText(MainActivity4.this,"向Fragment发送数据"+text,Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void thank(String code) {
Toast.makeText(MainActivity4.this,"已成功接收到"+code+"客气了!",Toast.LENGTH_SHORT).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"
android:id="@+id/send"/>
</LinearLayout>
package com.example.fragment;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import org.w3c.dom.Text;
public class MyFragment5 extends Fragment {
private String code = "Thank you! Activity";
public MyListener myListener;
public interface MyListener{
public void thank(String code);
}
//当Fragment添加到Activity中时调用这个方法,从而使得Fragment获取了当前的Activity,也就是需要发送回数据的Activity
@Override
public void onAttach(Activity activity) {
myListener = (MyListener) activity;
super.onAttach(activity);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment2,container,false);
TextView tv = view.findViewById(R.id.text);
//接收端获取数据包
String text = getArguments().get("name")+"";
tv.setText(text);
Toast.makeText(getActivity(),"已成功接收到"+text,Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(),"向Activity发送"+code,Toast.LENGTH_SHORT).show();
//从而调用MainActivity4中的thank方法
myListener.thank(code);
return view;
}
}
静态加载Fragment
package com.example.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
private RadioGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
group = (RadioGroup) findViewById(R.id.RadioGroup);
group.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.first:
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
break;
case R.id.second: {
MyFragment2 fragment2 = new MyFragment2();
FragmentManager fragmentManager = getFragmentManager();
//获取开启事务的对象
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
//第一个参数:要把fragment加载到哪个布局文件 第二个参数为fragment对象
beginTransaction.add(R.id.fragment, fragment2);
//通过物理返回可以回退到上个状态
beginTransaction.addToBackStack(null);
//事务提交
beginTransaction.commit();
break;
}
case R.id.third: {
break;
}
case R.id.fourth: {
Intent intent2=new Intent(this,MainActivity4.class);
startActivity(intent2);
break;
}
}
}
}
package com.example.fragment;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import javax.microedition.khronos.egl.EGLDisplay;
public class MainActivity4 extends Activity implements MyFragment5.MyListener {
private EditText editText;
private Button send;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main4);
editText = findViewById(R.id.editText);
send = findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText.getText().toString();
MyFragment5 myFragment5 = new MyFragment5();
Bundle bundle = new Bundle();
//传递字符串类型的字,key=name
bundle.putString("name",text);
myFragment5.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
//动态加载时设置fragment的tag
beginTransaction.add(R.id.layout,myFragment5,"fragment");
beginTransaction.commit();
Toast.makeText(MainActivity4.this,"向Fragment发送数据"+text,Toast.LENGTH_SHORT).show();
}
});
FragmentManager fragmentManager = getFragmentManager();
//获取了静态加载的Fragment对象
Fragment findFragmentById = fragmentManager.findFragmentById(R.id.frag);
//进行类型转换
MyFragment frag =(MyFragment)findFragmentById;
frag.setBbb("fragment静态传值");
}
@Override
public void thank(String code) {
Toast.makeText(MainActivity4.this,"已成功接收到"+code+"客气了!",Toast.LENGTH_SHORT).show();
}
}
main4.xml 含有加载的静态fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"
android:id="@+id/send"/>
<fragment
android:id="@+id/frag"
android:name="com.example.fragment.MyFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.example.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class MyFragment extends Fragment {
//设置需要传递的内容
private String bbb;
//设置对应的get,set方法
public String getBbb() {
return bbb;
}
public void setBbb(String bbb) {
this.bbb = bbb;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//将Layout布局文件转换为View对象
/*
* resource:需要加载的布局文件
* root:加载Layout的父ViewGroup
* attactToRoot:false,不返回父ViewGroup
* */
View view = inflater.inflate(R.layout.fragment,container,false);
TextView text = view.findViewById(R.id.text);
Button button = view.findViewById(R.id.Button);
text.setText("静态加载Fragment");
button.setText("获取内容");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value = getBbb();
Toast.makeText(getActivity(),"value="+value,Toast.LENGTH_SHORT).show();
}
});
return view;
}
}