从活动发送字符串到片段

时间:2019-04-08 09:48:49

标签: java android

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

活动:

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

6 个答案:

答案 0 :(得分:1)

在活动中,您将创建两个片段对象,您正在执行的第二个对象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();

答案 1 :(得分:1)

您必须先致电Bundle,然后再致电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, new SetttingsFragment())
                .commit();

答案 2 :(得分:1)

在您的代码中,将捆绑软件设置为不使用的片段变量

  1. 创建捆绑包
  2. 创建片段
  3. 在片段中设置捆绑包
  4. 显示片段

这是您需要在片段中传递参数的方式

public class SettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bundle = new Bundle();
    bundleSettings.putString("my_bundle_key", "Bundle");     
    SettingsFragment settingsFragment = new SettingsFragment();
    settingsFragment.setArguments(bundle);

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

答案 3 :(得分:0)

From Activity you send data with intent as:

Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
Fragmentclass obj = new Fragmentclass();
obj .setArguments(bundle);

在Fragment onCreateView方法中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
 String strtext = getArguments().getString("edttext");    
 return inflater.inflate(R.layout.fragment, container, false);
}

答案 4 :(得分:0)

  

活动中

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;
    }

答案 5 :(得分:0)

您需要将其添加为带有Fragments的参数,然后可以启动片段事务以启动fragment。此处使用的键为“ input”,此处为示例-

   Bundle bundle = new Bundle();
   bundle.putString("input", "dummy string args");
   MapFragment mapFragment = new MapFragment();
   mapFragment.setArguments(bundle); 

并接收参数,即输入fragment内,像-

一样使用它
String data =  getArguments().getString("input")