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

我尝试将字符串从活动传递到片段。如您所见,我正在使用if语句来防止应用程序崩溃。Toast 消息始终显示“捆绑空”。如何防止联邦为空?


活动:


public class SettingsActivity extends AppCompatActivity {


@Override

protected 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  {

@Override

public 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();

    }


喵喵时光机
浏览 153回答 5
5回答

呼啦一阵风

在代码中,您将捆绑包设置为未使用的片段变量创建捆绑包创建片段在片段中设置捆绑包显示片段这是您需要以片段形式传递参数的方式public class SettingsActivity extends AppCompatActivity {@Overrideprotected 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();

收到一只叮咚

在调用之前,您必须调用捆绑包,如下所示:fragmentBundle 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();

Cats萌萌

在您的活动中,您正在创建两个片段对象,您正在执行的第二个对象未附加到视图且未在使用中。您的片段应按如下方式附加到视图:setargumentBundle 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();

一只萌萌小番薯

From Activity you send data with intent as:Bundle bundle = new Bundle();bundle.putString("edttext", "From Activity");Fragmentclass obj = new Fragmentclass();obj .setArguments(bundle);在片段创建视图方法中:@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) { String strtext = getArguments().getString("edttext");     return inflater.inflate(R.layout.fragment, container, false);}

慕雪6442864

活动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;    }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java