How to transfer data via Bundle to static fragment from Activity?

Activity.class

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Bundle bundle=new Bundle(); bundle.putString("name", "From Activity"); Fragment1 fragobj=new Fragment1(); fragobj.setArguments(bundle); } } 

Frament.class

 public class Fragment1 extends Fragment { final String LOG_TAG = "myLogs"; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Bundle bundle = this.getArguments(); String myValue = bundle.getString("name"); Log.d(LOG_TAG, "My Bundle contain "+ strtext); return inflater.inflate(R.layout.fragment1, container, false); } } 

fragment.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#77ff0000" android:orientation="vertical"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="frag1_text"> </TextView> </LinearLayout> 

activity_main.mxl

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="HELLO I am ACTIVITY "/> <fragment android:id="@+id/fragment1" android:name="com.my_app.one.fragment_bundle.Fragment1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" tools:layout="fragment1"> </fragment> </LinearLayout> 

Crashes with an error on the line setContentView (R.layout.activity_main); java.lang.RuntimeException: Unable to start activity ComponentInfo

bundle = null

  • one
    Remove the fragment from the markup and add the manager through the fragment, then everything will turn out - temq

2 answers 2

You have everything implemented through the fragment. Easier to mark in the markup

 <FrameLayout android:id="@+id/fmContent" android:layout_width="match_parent" android:layout_height="match_parent"/> 

In activit in onCreate or where it is convenient

 Bundle bundle=new Bundle(); bundle.putString("name", "From Activity"); Fragment1 fragobj=new Fragment1(); fragobj.setArguments(bundle); getSupportFragmentManager().beginTransaction() .replace(R.id.fmContent, fragobj) .commit(); 

in the fragment it is advisable to check:

 if (getArguments != null && getArguments().containsKey("name")) { //do smth } 
  • “How to transfer data through the Bundle to a static fragment from the Activity?” - post_zeew
  • For static, you can do this: In the fragment, add a public method public void setData (Yurdata data) { } From Activate, call ((MyFragment)getSupportFragmentManager().findFragmentById(id)).setData(); - TooLazy

You need to create a static method that will create a fragment and give it to the caller.

  public MyFragment extends Fragment { private static final String BUNDLE_CONTENT = "bundle_content" public static MyFragment newInstance(final String content) { final MyFragment fragment = new MyFragment(); final Bundle arguments = new Bundle(); arguments.putString(BUNDLE_CONTENT, content); fragment.setArguments(arguments); return fragment; } private String content; public MyFragment() { //default constructor } //В onCreate() мы прочитаем переданный Bundle @Override protected void onCreate(Bundle saveInstanceState) { if (getArguments() != null && getArguments().containsKey(BUNDLE_CONTENT)) { content = getArguments().getString(BUNDLE_CONTENT); } else { throw new IllegalArgumentException("Must be created through newInstance(...)"); } } ... } 

Thus, the fragment call will be like this:

 MyFragment fragment = MyFragment.newInstance("something"); 

And the fragment will receive information

  • content = null and FATAL EXCEPTION: java.lang.RuntimeException: Unable to start activity ComponentInfo during the initialization phase layout Caused by: java.lang.IllegalArgumentException: Must be created through newInstance (...) - mtb
  • Show the code you create. Must be something like MyFragment fragment = MyFragment.newInstance ("something"); - Andrew Grow