今天又学了Activity通过Argument向Fragment传递数据,这种方式是安卓给我们提供的,相比与用方法传递,它能传递更多的数据,更加常用。
Argument传递数据需要使用Bundle容器,传递的实际上是键值对,形式上有点类似intent和servlet里的session。
代码实现:
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 ArgumentDataClick(View view) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); BlankFragment2 blankFragment2 = new BlankFragment2(); Bundle bundle = new Bundle(); bundle.putString("data","这是通过Argument传递的数据"); bundle.putInt("int_data",168); blankFragment2.setArguments(bundle); fragmentTransaction.replace(R.id.frag_dy_1,blankFragment2).commit(); } }
我们需要在fragment 中创建全局变量,
然后重写onCreate方法
data = getArguments().getString("data");
然后onViewCreated加入
if (!TextUtils.isEmpty(data)){ mTextView.setText(data+int_data); }
用于给textview赋值
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 String data; private int int_data; 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); data = getArguments().getString("data"); int_data = getArguments().getInt("int_data"); } } @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); } if (!TextUtils.isEmpty(data)){ mTextView.setText(data+int_data); } } }
标签:Fragment,Argument,Bundle,fragment,传递数据,import,BlankFragment2,data,public From: https://www.cnblogs.com/rsy-bxf150/p/17254781.html