Activity通过构造方法和普通方法向Fragment传递参数

发布时间 2023-03-25 14:48:35作者: 冰稀饭Aurora

今天学习了Activity通过构造方法和普通方法向Fragment传递参数

这种方式的缺点是传递数据量较少

看一下效果:

 

 代码实现:

这里示例了两种方法,一个是用构造方法,一个是普通方法

activity

package com.example.dataapplication;

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 com.example.dataapplication.fragment.BlankFragment1;
import com.example.dataapplication.fragment.BlankFragment2;

public class DynamicFragmentActivity extends AppCompatActivity {

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

        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.frag_dy_1,BlankFragment2.class, null)
                .commit();

    }

    public void passDataClick(View view) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction =  fragmentManager.beginTransaction();
        BlankFragment2 blankFragment2 = new BlankFragment2("这是构造方法传递的数据");
        fragmentTransaction.replace(R.id.frag_dy_1,blankFragment2).commit();
    }

    public void passDataClickNormal(View view) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.frag_dy_1);

        if (fragment != null){
            ((BlankFragment2)fragment).setArgParam1("这是普通public传递的数据");
        }
    }
}

fragment

package com.example.dataapplication.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.dataapplication.R;

public class BlankFragment2 extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    private TextView mTextView;
    public BlankFragment2(String data) {
        mParam1 = data;
    }
    public BlankFragment2() {
    }
    public void setArgParam1(String data){
        this.mParam1 = data;
        if (!TextUtils.isEmpty(mParam1)){
            mTextView.setText(mParam1);
        }
    }
    public static BlankFragment2 newInstance(String param1, String param2) {
        BlankFragment2 fragment = new BlankFragment2();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_blank2, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mTextView = view.findViewById(R.id.item_frag1);
        if (!TextUtils.isEmpty(mParam1)){
            mTextView.setText(mParam1);
        }
    }
}