如何在片段中启动片段/活动?

时间:2013-04-12 10:28:44

标签: android android-fragments android-fragmentactivity

enter image description here

我在Fragment中有一个按钮。点击Button后,必须在Fragment/Activity内打开新的Fragment。我用Intent编写了代码,                              Intent i = new Intent(); i.setClass(getActivity(), UpdateProfile.class); startActivity(i); 但它在新活动中的开放,如下图所示。 enter image description here

我的要求如图1所示。有人可以建议我怎么做吗?

编辑:正如rai和ADK所建议的那样,它的工作正常,但新片段覆盖在旧片段上。见下图。 “更改密码”(TextView)是新片段,它覆盖在现有片段上。

enter image description here

2 个答案:

答案 0 :(得分:3)

尝试:

getFragmentManager()
    .beginTransaction()
    .replace(containerViewId, newFragment)
    .addToBackStack(null) // enables back key
    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) // if you need transition
    .commit();

答案 1 :(得分:1)

您应该使用FragmentTransaction enter link description here

喜欢这个

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.yourFragment, YourFragmentWithImageClass.getInstance());
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        fragmentTransaction.commit();

在你的活动中

相关问题