将FragmentActivity中的字符串传递给Fragment

时间:2013-08-03 06:51:10

标签: android android-fragments android-fragmentactivity

我能够从Activity中获取FragmentActivity中的String。但是,如何将我的数据从FragmentActivity传递给Fragments?我可以在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();
        }
    }
}

3 个答案:

答案 0 :(得分:1)

在您的片段中,您可以通过调用以下方法getActivity()来获取FragmentActivity。在FragmentActivity中创建一个类似getValue()的方法,它将返回所需的数据。

答案 1 :(得分:0)

文档建议您使用片段的父活动。您不应该使用片段来进行片段通信,以便您可以在必要时在其他位置重用该片段并使其可重复使用。话虽如此,我建议使用放在捆绑上的Extras。

你说的是这样的:

savedInstanceState.putString("myString", myStingVariable);

你可以这样得到它们:

myStingVariable = savedInstanceState.getString("myString");

您还需要检查该值是否为空。

请注意,不同类型的变量有不同的getter和setter。

希望这有帮助。

答案 2 :(得分:0)

您可以在片段中创建一个setter方法,如下所示。从您的活动中,只需调用该方法。

public class IndividualDetailsBaseAdapter extends FragmentPagerAdapter {
protected static final String[] CONTENT = new String[] { "Comments",
        "Details", "Screens", "Tutorial" };

private int mCount = CONTENT.length;

private String name;
public void setName(String name)
{
  this.name=name;
}


public IndividualDetailsBaseAdapter(FragmentManager fm) {
    super(fm);
}
 ... // Rest of your stuff.
 }

您可以从您的Activity中调用setName方法,如下所示

mAdapter = new IndividualDetailsBaseAdapter(getSupportFragmentManager());
mAdapter.setName(getIntent().getStringExtra("name"));