Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment getArguments() returns null

Tags:

I have a Fragment which has a TabHost as the root layout as follows...

<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@android:id/tabhost"     android:layout_width="fill_parent"     android:layout_height="fill_parent">      <LinearLayout         android:orientation="vertical"         android:layout_width="fill_parent"         android:layout_height="fill_parent">          <TabWidget             android:id="@android:id/tabs"             android:layout_width="fill_parent"             android:layout_height="wrap_content" />          <FrameLayout             android:id="@android:id/tabcontent"             android:layout_width="fill_parent"             android:layout_height="fill_parent">              <FrameLayout                 android:id="@+id/tab_1"                 android:layout_width="fill_parent"                 android:layout_height="fill_parent" />              <!-- More FrameLayouts here - each are placeholders for Fragments -->              </FrameLayout>     </LinearLayout> </TabHost> 

The code to create / update each Fragment for the tab content is as follows...

private void updateTab(String tabId, int placeholder) {     FragmentManager fm = getFragmentManager();     if (fm.findFragmentByTag(tabId) == null) {         Bundle arguments = new Bundle();         arguments.putInt("current_day", mCurrentTab);         EpgEventListFragment fragment = new EpgEventListFragment();         fragment.setArguments(arguments);          fm.beginTransaction()                 .replace(placeholder, new EpgEventListFragment(), tabId)                 .commit();     } } 

In the onCreate(...) method of the EpgEventListFragment I then try to get the arguments Bundle but I always get null doing the following...

@Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      Bundle arguments = getArguments();     if (arguments == null)         Toast.makeText(getActivity(), "Arguments is NULL", Toast.LENGTH_LONG).show();     else         mCurrentDay = getArguments().getInt("current_day", 0);      ... } 

What am I missing here? I also tried getArguments() in onAttach(...) but I still got null. I'm new to using Fragments so I'm hoping there's a simple reason but I haven't come up with anything when searching.

like image 589
Squonk Avatar asked Feb 20 '13 01:02

Squonk


1 Answers

I'm thinking this has to do with your problem:

fm.beginTransaction()     .replace(placeholder, new EpgEventListFragment(), tabId)     .commit(); 

You're making a new Fragment (that doesn't have arguments since it's been freshly instantiated).

Instead try

Fragment fragment = new EpgEventListFragment(); fragment.setArguments(arguments); fm.beginTransaction()     .replace(placeholder, fragment, tabId)     .commit(); 
like image 123
A--C Avatar answered Oct 13 '22 04:10

A--C