我能夠從Activity中獲取FragmentActivity中的字符串。但是,如何將我的數據從FragmentActivity傳遞到碎片?儘管我能夠在FragmentActivity中獲取數據。將FragmentActivity中的字符串傳遞給片段
(我不想將數據存儲在SharedPreferences。)
public class IndividualDetailsActivity extends IndividualBasePager {
ViewPager mPager;
IndividualDetailsBaseAdapter mAdapter;
TitlePageIndicator mIndicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.individual_pager);
mAdapter = new IndividualDetailsBaseAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setOffscreenPageLimit(4);
mPager.setAdapter(mAdapter);
mIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
mIndicator.setCurrentItem(1);
Intent i = getIntent();
getSupportActionBar().setTitle(i.getStringExtra("name"));
}
}
public class IndividualBasePager extends SherlockFragmentActivity {
IndividualDetailsBaseAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
setSupportProgressBarIndeterminateVisibility(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class IndividualDetailsBaseAdapter extends FragmentPagerAdapter {
protected static final String[] CONTENT = new String[] { "Comments",
"Details", "Screens", "Tutorial" };
private int mCount = CONTENT.length;
public IndividualDetailsBaseAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Log.e("PAGE", "Position " + position);
switch (position) {
case 0:
return new CommentsFragment();
case 1:
return new DetailsFragment();
case 2:
return new CommentsFragment();
case 3:
return new CommentsFragment();
}
return null;
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
return IndividualDetailsBaseAdapter.CONTENT[position % CONTENT.length];
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}