为了账号安全,请及时绑定邮箱和手机立即绑定

将字符串从活动发送到片段

将字符串从活动发送到片段

喵喵时光机 2022-09-28 09:57:12
我尝试将字符串从活动传递到片段。如您所见,我正在使用if语句来防止应用程序崩溃。Toast 消息始终显示“捆绑空”。如何防止联邦为空?活动:public class SettingsActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    getSupportFragmentManager().beginTransaction()            .replace(android.R.id.content, new SetttingsFragment())            .commit();    Bundle bundle = new Bundle();    bundleSettings.putString("my_bundle_key", "Bundle");         SetttingsFragment setttingsFragment = new SetttingsFragment();    setttingsFragment.setArguments(bundle);片段:public class SetttingsFragment extends PreferenceFragmentCompat  {@Overridepublic void onCreatePreferences(Bundle bundle, String rootKey) {    setPreferencesFromResource(R.xml.preferences, rootKey);    Bundle bundle = getArguments();    if(bundle != null){        String bundleString = bundle.getString("my_bundle_key");        Log.i("my_bundle_key", bundleString);               } else{        Toast.makeText(getActivity(), "Bundle null", Toast.LENGTH_LONG).show();    }
查看完整描述

5 回答

?
呼啦一阵风

TA贡献1802条经验 获得超6个赞

在代码中,您将捆绑包设置为未使用的片段变量

  1. 创建捆绑包

  2. 创建片段

  3. 在片段中设置捆绑包

  4. 显示片段

这是您需要以片段形式传递参数的方式

public class SettingsActivity extends AppCompatActivity {


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    SettingsFragment settingsFragment = new SettingsFragment();

    if (savedInstanceState == null) {

        Bundle bundle = new Bundle();

        bundle.putString("my_bundle_key", "Bundle");     

        settingsFragment.setArguments(bundle);

    }

    else {

        settingsFragment.setArguments(savedInstanceState);

    }

    getSupportFragmentManager().beginTransaction()

        .replace(android.R.id.content, settingsFragment)

        .commit();


查看完整回答
反对 回复 2022-09-28
?
收到一只叮咚

TA贡献1821条经验 获得超5个赞

在调用之前,您必须调用捆绑包,如下所示:fragment

Bundle bundle = new Bundle();
        bundleSettings.putString("my_bundle_key", "Bundle");     
        SetttingsFragment setttingsFragment = new SetttingsFragment();
        setttingsFragment.setArguments(bundle);

    getSupportFragmentManager().beginTransaction()
                .replace(android.R.id.content, setttingsFragment)
                .commit();


查看完整回答
反对 回复 2022-09-28
?
Cats萌萌

TA贡献1805条经验 获得超9个赞

在您的活动中,您正在创建两个片段对象,您正在执行的第二个对象未附加到视图且未在使用中。您的片段应按如下方式附加到视图:setargument

Bundle bundle = new Bundle();
bundleSettings.putString("my_bundle_key", "Bundle");     
SetttingsFragment setttingsFragment = new SetttingsFragment();
setttingsFragment.setArguments(bundle);

  getSupportFragmentManager().beginTransaction()
        .replace(android.R.id.content, setttingsFragment)
        .commit();


查看完整回答
反对 回复 2022-09-28
?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

From Activity you send data with intent as:


Bundle bundle = new Bundle();

bundle.putString("edttext", "From Activity");

Fragmentclass obj = new Fragmentclass();

obj .setArguments(bundle);

在片段创建视图方法中:


@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

    Bundle savedInstanceState) {

 String strtext = getArguments().getString("edttext");    

 return inflater.inflate(R.layout.fragment, container, false);

}


查看完整回答
反对 回复 2022-09-28
?
慕雪6442864

TA贡献1812条经验 获得超5个赞

活动


AbcFragment fragment = AbcFragment .newInstance(**Value**);

               getSupportFragmentManager.beginTransaction()

                        .replace(R.id.layout_container, fragment)

                        .commit();

在片段中


public static AbcFragment newInstance(String Value) {

        Bundle args = new Bundle();


        args.putString("Value", Value);


        AbcFragment fragment = new AbcFragment ();

        fragment.setArguments(args);

        return fragment;

    }


查看完整回答
反对 回复 2022-09-28
  • 5 回答
  • 0 关注
  • 152 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号