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