AS中級 フラグメント編③~Fragmentの真骨頂、同じフラグメントを異なるアクティビティに配置する~

MainActivity.java

package com.example.realfragmentdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

TopFragment topFragment = TopFragment.newInstance();
MiddleFragment middleFragment = new MiddleFragment();

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container1_1,topFragment);
fragmentTransaction.add(R.id.container1_2,middleFragment);
fragmentTransaction.commit();

Button nextBtn = findViewById(R.id.nextBtn);
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,SecondActivity.class));
}
});


}
}

activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/container1_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/container1_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="次ページへ"/>

</LinearLayout>

SecondActivity.java

package com.example.realfragmentdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

public class SecondActivity extends AppCompatActivity {

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

MiddleFragment middleFragment = MiddleFragment.newInstance();
BottomFragment bottomFragment = BottomFragment.newInstance();

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container2_1,middleFragment);
fragmentTransaction.add(R.id.container2_2,bottomFragment);
fragmentTransaction.commit();

}
}

activity_second.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/container2_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/container2_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>

</LinearLayout>

TopFragment.java

package com.example.realfragmentdemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class TopFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_top, container, false);
}

public static TopFragment newInstance(){
TopFragment topFragment = new TopFragment();
return topFragment;
}
}

fragment_top.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".TopFragment">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"/>

</LinearLayout>

MiddleFragment.java

package com.example.realfragmentdemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class MiddleFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_middle, container, false);
}

public static MiddleFragment newInstance(){
MiddleFragment middleFragment = new MiddleFragment();
return middleFragment;
}
}

fragment_middle.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF00FF"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MiddleFragment">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hi"/>

</LinearLayout>

BottomFragment.java

package com.example.realfragmentdemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

public class BottomFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_bottom, container, false);
}

public static BottomFragment newInstance(){
BottomFragment bottomFragment = new BottomFragment();
return bottomFragment;
}
}

fragment_botton.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".BottomFragment">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Bye"/>

</LinearLayout>