LoginSignup
8
7

More than 5 years have passed since last update.

fragment遷移時、画面が重なり前のページが機能してしまう場合の対処法

Posted at

参考にしたサイト

YAMの雑記帳様
http://y-anz-m.blogspot.com/2013/05/support-package-fragment.html

対処法

一度frameLayoutのみのxmlをsetContentViewでセットしてそこを基準にfragment遷移していく。

[main_fragment.xml]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />



</LinearLayout>

[MainActivity.java]//oncreate内


 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_fragment);

        CalendarFragment calendarFragment = new CalendarFragment();
        //FragmentManagerをセット
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager
                .beginTransaction();
        transaction.addToBackStack(null);

        transaction.replace(R.id.container, calendarFragment);
        transaction.commit();
}

これで透過や重なりを防げる。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7