Activity通过构造方法和普通方法向Fragment传递参数
Posted Aurora-RenShuoyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activity通过构造方法和普通方法向Fragment传递参数相关的知识,希望对你有一定的参考价值。
今天学习了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);
以上是关于Activity通过构造方法和普通方法向Fragment传递参数的主要内容,如果未能解决你的问题,请参考以下文章