Activity中跳转fragment后,两个页面内容同时出现??

user_biin 2021-05-27 12:53:06
求助帖
android 在activity中设置一个按钮,监听点击跳转fragment,但是没想到跳转后两个页面内容同时展示了,大佬们这个问题我该怎么解决呢!!

@Override
public void onClick(View v) {
androidx.fragment.app.FragmentManager fragmentManager = getSupportFragmentManager();
androidx.fragment.app.FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
switch (v.getId()) {
case R.id.frg1:
Log.d(TAG, "点击跳转事件");
beginTransaction.replace(R.id.main_re, new BlankFragment());
beginTransaction.addToBackStack(null);
break;
}
beginTransaction.commit();
// moveTaskToBack(true);
}


这是MainActivity布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/main_re"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/frg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="跳转"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="248dp"
tools:layout_editor_absoluteY="295dp" />

</FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>


这是运行结果


...全文
274 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
立青_ 中级 2021-05-27
  • 打赏
  • 举报
回复
fragment本身就依附于Activity的,这不能称之为跳转,你只是吧fragment放进Activity的这个容器里,如果容器不能遮住Activity的所有布局,那不就是同时显示了诺
不会写代码的猴子 版主 2021-05-27
  • 打赏
  • 举报
回复
先隐藏,再显示

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧