4.4activity和fragment之间的通信

他俩是独立的生命个体,关系其实并没有那么密切,通信不是很方便

1.activity 把信息传递给 fragment 

Android的原生方案是利用 Bundle进行通信 

Bundle可以理解为一个可以保存数据的类

bundle类中加入数据(key -value的形式,另一个activity里面取数据的时候,就要用到key,找出对应的value)

bundle.putXXX  可以put好多类型

 

具体例子如下:

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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="换一个fragment"
        />

    <FrameLayout
        android:id="@+id/myFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

fragment_blank.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"
    tools:context=".BlankFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff0000"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>
BlankFragment.class
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.Toast;

public class BlankFragment extends Fragment {

    public BlankFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments(); String myMessage = bundle.getString("myMessage"); Toast.makeText(getContext(),myMessage,Toast.LENGTH_SHORT).show(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_blank, container, false); } }

 

MainActivity.class
package com.example.activitygivefragmentmessage;

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;

public class MainActivity extends AppCompatActivity {

    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.btn1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("myMessage","这是activity传来的参数");
                BlankFragment blankFragment = new BlankFragment();
                blankFragment.setArguments(bundle);
                changeFragment(blankFragment);
            }
        });
    }

    private void changeFragment(Fragment fragment) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.myFrameLayout,fragment);
        fragmentTransaction.commit();
    }
}

 

 

 

上一篇:Fragment的通信,图文详解


下一篇:Android事件体系全面总结+实践分析,实践出真知