我想将意图值传递给片段,现在在活动中我通过 onNewIntent 获取意图值,但我通过 Bundle 将意图值传递给片段,但它不起作用。
* MainActivity.java *
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
parseMessage = intent.getStringExtra("message");
if (parseMessage.length()>0) {
Bundle bundle = new Bundle();
bundle.putString("parseJson", parseMessage);
AbcdFragment activityFrag = new AbcdFragment();
activityFrag.setArguments(bundle);
}
}
@SuppressLint("DefaultLocale")
public class AllPagerAdapter extends FragmentPagerAdapter {
public AllPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
return AbcdFragment.newInstance(getApplicationContext());
case 1:
return HelloFragment.newInstance(getApplicationContext());
default:
return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
return CONTENT[position].toUpperCase();
}
@Override
public int getCount() {
return 2;
}
}
* AbcdFragment.java *
public class AbcdFragment extends Fragment{
public static AbcdFragment newInstance(Context context) {
AbcdFragment fragment = new AbcdFragment();
fragment.context = context;
return fragment;
}
@SuppressLint("InflateParams")
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.abcd_activity,container,false);
try {
parseJsonRes = getArguments().getString("parseJson");
Log.d("ParseActivityMsg", parseJsonRes);
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
}
这是一个视图页面片段,我想将意图值传递给 AbcdFragment,但我在 AbcdFragment 中得到空值。
请仔细阅读我的代码并建议我一些解决方案