android片段getArguments在oncreateView()中返回null

时间:2015-10-07 10:11:00

标签: android null return arguments fragment

我有两个片段FilterFragmentLocationListFragment。当我切换到LocationListFragment.java时,我通过调用getArguments()获取LocationListFragment.javaFilterFragment.javagetArgument()的值,获取空值这是细节

FilterFargment.java代码中有设置值:

  LocationListFragment locationListFragment = new LocationListFragment();
             bundle = new Bundle();
            Log.e("",""+locationList.getLocationListFilter().size());
            bundle.putParcelable(Constant.LOCATION_LIST_FILTER, locationList);
            bundle.putString(Constant.FROM_DATE_FILTER, textView_time_from.getText().toString());
            locationListFragment.setArguments(bundle);
            ((DashbordActivity) getActivity()).switchFragment(locationListFragment, true);

LocationListFragment.java(获取值)

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle args = getArguments();
    if (args != null) {
        locationList = args.getParcelable(Constant.LOCATION_LIST_FILTER);
        getFromDate = args.getString(Constant.FROM_DATE_FILTER);}}

LocationListFragment点按钮上点击发送数据到FilterFragment发送包含新值的数据:

FilterFragment filterFragment =new FilterFragment();//.newInstance(getFromDate, getEndDate, getLocation, getCategories);
    Bundle bundle = new Bundle();
    bundle.putString(Constant.FROM_DATE_FILTER, getFromDate);
    bundle.putString(Constant.WHERE_FILTER, getLocation);
    filterFragment.setArguments(bundle);
    ((DashbordActivity) getActivity()).switchFragment(filterFragment, true);

并在FilterFragment.java返回时,getArguments();代码的 null 值为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_filter, container, false);
    Log.e("FilterFragment", "oncreateview");

    Bundle args = getArguments();
    Log.e("getArguments", "" + getArguments());//getting null here

另外,我已经使用这种方法来切换片段,

    public void switchFragment(BaseFragment fragment, boolean keep) {
    FragmentManager manager = getSupportFragmentManager();

    boolean fragmentPopped = manager.popBackStackImmediate(fragment.getClass().getSimpleName(), 0);

    if (!fragmentPopped && manager.findFragmentByTag(fragment.getClass().getSimpleName()) == null) { //fragment not in back stack, create it.

        FragmentTransaction tr = manager.beginTransaction();

        tr.replace(R.id.container, fragment, fragment.getClass().getSimpleName());

        if (keep) {
            tr.addToBackStack(fragment.getClass().getSimpleName());
       Log.e("keep", "BaseAct:" + keep);
        }
        tr.commit();
    }
}

你能告诉我我错过了什么吗?任何帮助将不胜感激。

0 个答案:

没有答案