How to pass Arguments to Fragment from Activity

后端 未结 4 1199
既然无缘
既然无缘 2021-01-05 18:37

I have an activity which instantiates and populates a Fragment through a adopter class. I want to pass values/objects to this Fragment adopter class so that i can dictate la

4条回答
  •  死守一世寂寞
    2021-01-05 18:46

    It's hard to say without seeing the logcat, but surely you are missing to call the commit() method on the FragmentTransaction. Also remember to set the arguments to the fragment before calling ft.commit().

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        mFragment = (LayoutFragment) getSupportFragmentManager().findFragmentByTag(mTag);
    
        if (mFragment == null) {
            Bundle bundle = new Bundle();
            bundle.putInt("noTiles", 4);
            mFragment = (LayoutFragment) LayoutFragment.newInstance(mLayoutId);
            mFragment.setArguments(bundle);
            //ft.add(R.id.content, mFragment, mTag).commit();
            ft.add(R.id.content, mFragment, mTag);
    
        } else {
            ft.attach(mFragment);
        }
    
        mSelectedLayoutId = mFragment.getLayoutId();
    }
    

提交回复
热议问题