通过Bundle可以将选中的位置从Activity传递给FragmentActivity
您的 Intent 应如下所示:
Intent intent = new Intent(Activity.this, FragmentActivity.class);
intent.putExtra("idforthevalue",selectedPOsition);
startActivity(intent);
然后您可以在 FragmentActivity 上检索该值:
Bundle extras = getIntent().getExtras();
int position = 0;
if(extras != null) {
position = extras.getInt("idforthevalue");
}
根据您添加片段的方式,您还可以使用 FragmentTransaction 中的 FragmentActivity 中的 Bundle 将此值传递给它们
FragmentManager fragmentManager = getFragmentManager(); // or getSupportFragmentManager() if you are using compat lib
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentX fragmentX = new FragmentX();
Bundle bundle = new Bundle();
bundle.putInt("idforthevalue", position);
fragmentX.setArguments(bundle);
fragmentTransaction.replace(id_of_container, fragmentX).commit();
你可以再次检索片段中的值
Bundle bundle = getArguments();
if(bundle != null) {
position = bundle.getInt("idforthevalue", 0);
}
您可以对三个 Fragment 执行相同的操作。