后退按钮(活动)返回片段

时间:2016-06-17 21:11:07

标签: android

我有一个extends FragmentActivity

的课程

使用以下内容访问片段:

@Override
public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub

    Fragment fragment=null;
    if (arg0 == 0) {
        fragment = new Fragment_01_Rooms();
    }
    if (arg0 == 1) {
        fragment = new Fragment_02_Contacts();
    }

    return fragment;
}

现在调用的片段是public class Fragment_02_Contacts extends Fragment

通过以下方式通过意图调用另一个类:

Intent intent = new Intent(getActivity(), ActivityChatPrivate.class);
intent.putExtra("valueCONTACT", contactSelected);
startActivity(intent);

现在这个名为public class ActivityChatPrivate extends FragmentActivity

的班级

在这堂课上,我试图通过后退按钮返回Fragment_02_Contacts。我怎么做

public void onBackPressed(){
    //some_code_here to return back to Fragment_02_Contacts 
}

1 个答案:

答案 0 :(得分:0)

试试这个

Intent intent = new Intent(getActivity(), ActivityChatPrivate.class);
intent.putExtra("valueCONTACT", contactSelected);
intent.putExtra("Fragment", "Fragment2");
startActivity(intent);

现在在public class ActivityChatPrivate

String returnToFragment = getIntent().getExtras().getString("Fragment");

onBackPressed

switch(returnToFragment){
  case "Fragment2"
  Fragment_02_Contacts fragment2 = new Fragment_02_Contacts(); 
  FragmentTransaction transaction = getFragmentManager().beginTransaction();
  transaction.replace(R.id.fragment_container, newFragment);
  transaction.commit(); 
break;
}