从活动导航到片段

时间:2017-12-22 06:31:58

标签: android android-fragments

如何通过后退按钮从一个活动导航到另一个活动fragment? 如果你知道答案,请帮助我。我是Android的新手。

1 个答案:

答案 0 :(得分:0)

您可以使用从Fragment切换到Activity - :

在BackPressed上实现此代码 - :

@Override
    public void onBackPressed() {
        super.onBackPressed();

     YourFragment mapFragment = new YourFragment();
    FragmentManager manager = getFragmentManager();
    manager.beginTransaction().replace(R.id.landlord_apartment, mapFragment, mapFragment.getTag()).commit();
    }

从片段转移到活动 - :

startActivity(new Intent(getActivity,YourActivity.Class));

从活动转移到片段 - :

YourFragment mapFragment = new YourFragment();
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.landlord_apartment, mapFragment, mapFragment.getTag()).commit();