如何从活动中恢复片段?

时间:2015-06-15 06:36:17

标签: android android-fragments

我在活动C中有四个片段。它们表现为制表符。我必须从一个片段转到一个新的Activity X.现在我想回到片段从Activity X片段化。

这是我的主要活动

'public class MainInterface扩展了ActionBarActivity {

ViewPager pager;
PagerTabStrip tab_strp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_interface);
    MainPagerAdapter mapager = new MainPagerAdapter(getSupportFragmentManager());
    pager = (ViewPager) findViewById(R.id.pager);

    pager.setAdapter(mapager);
    tab_strp = (PagerTabStrip) findViewById(R.id.tab_strip);
    //tab_strp.setTextColor(Color.WHITE);
    //tab_strp.setTextSize(14,14);
     //tab_strp.setTabIndicatorColor(Color.WHITE);
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2196f3")));
    getSupportActionBar().setTitle("Instructor");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    //getSupportActionBar().setHomeButtonEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; goto parent activity.
            this.finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

} “

这里是活动
'public class Discussions extends Fragment实现了View.OnClickListener {

ImageButton post;
TextView dTitle;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view =inflater.inflate(R.layout.discussions,container,false);
    post=(ImageButton)view.findViewById(R.id.ibDisc);
    post.setOnClickListener(this);

    dTitle=(TextView)view.findViewById(R.id.tvDiscTitle);

    return view;
}

@Override
public void onClick(View view) {
    Intent in=new Intent(getActivity(),PostDiscussion.class);
    startActivity(in);

}

}“

1 个答案:

答案 0 :(得分:1)

enter image description here在导航到新活动之前将片段的名称保存在sharedPreferences中,并在该新活动的onBackpressed上或当您想要返回到相同的片段时从SharedPreferences获取名称并将该特定片段添加到更早的活性