我有两个片段FilterFragment
和LocationListFragment
。当我切换到LocationListFragment.java
时,我通过调用getArguments()
获取LocationListFragment.java
和FilterFragment.java
到getArgument()
的值,获取空值这是细节
在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();
}
}
你能告诉我我错过了什么吗?任何帮助将不胜感激。