안드로이드 앱 만들기
Fragment끼리 데이터 전송
khon98
2020. 9. 22. 22:50
Fragment에선 findViewById가 바로 작동이 안됨 findViewById앞에 view를 붙여야 함
------------------------
activity_main.xml
<?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/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
------------------------
xml 파일 생성
1. res 폴더 > layout 폴더 우클릭 > File name: fragment_1 >
Root element: androidx.constraintlayout.widget.ConstraintLayout
2. design 탭에서 작업
TextView 생성 > Button 생성
------------------------
fragment_1.xml
<?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">
<TextView
android:id="@+id/tv_frag1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="100dp"
android:text="프래그먼트 1"
android:textSize="36sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_move"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="프래그먼트 2로 이동"
app:layout_constraintEnd_toEndOf="@+id/tv_frag1"
app:layout_constraintStart_toStartOf="@+id/tv_frag1"
app:layout_constraintTop_toBottomOf="@+id/tv_frag1" />
</androidx.constraintlayout.widget.ConstraintLayout>
------------------------
xml 파일 복사 후 붙여넣기
------------------------
fragment_2.xml
<?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">
<TextView
android:id="@+id/tv_frag2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="100dp"
android:text="프래그먼트 2"
android:textSize="36sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_move"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="프래그먼트 1로 이동"
app:layout_constraintEnd_toEndOf="@+id/tv_frag2"
app:layout_constraintStart_toStartOf="@+id/tv_frag2"
app:layout_constraintTop_toBottomOf="@+id/tv_frag2" />
</androidx.constraintlayout.widget.ConstraintLayout>
------------------------
------------------------
MainActivity.java
package com.example.fragmentbundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment1 fragment1 = new Fragment1();
transaction.replace(R.id.framelayout, fragment1);
transaction.commit(); // 저장
}
}
------------------------
java 파일 생성
------------------------
Fragment1.java
package com.example.fragmentbundle;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
public class Fragment1 extends Fragment {
private View view;
private TextView tv_frag1;
private Button btn_move;
private String result;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_1, container, false);
tv_frag1 = view.findViewById(R.id.tv_frag1);
btn_move = view.findViewById(R.id.btn_move);
if (getArguments() != null) { // null : 빈 값
result = getArguments().getString("fromFrag2"); // 프래그먼트 1로 부터 setArguments된 데이터를 받아옴
tv_frag1.setText(result);
}
btn_move.setOnClickListener(new View.OnClickListener() { // 프래그먼트 2로 이동
@Override
public void onClick(View view) {
Bundle bundle = new Bundle(); // 무언가를 담을 준비를 할 수 있는 보따리 or 꾸러미 같은 느낌
bundle.putString("fromFrag1","khon 프래그먼트 1");
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
Fragment2 fragment2 = new Fragment2();
fragment2.setArguments(bundle);
transaction.replace(R.id.framelayout, fragment2);
transaction.commit(); // 저장
}
});
return view;
}
}
------------------------
java 파일 복사 후 붙여넣기
------------------------
Fragment2.java
package com.example.fragmentbundle;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
public class Fragment2 extends Fragment {
private View view;
private TextView tv_frag2;
private Button btn_move;
private String result;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_2, container, false);
tv_frag2 = view.findViewById(R.id.tv_frag2);
btn_move = view.findViewById(R.id.btn_move);
if (getArguments() != null) { // null : 빈 값
result = getArguments().getString("fromFrag1"); // 프래그먼트 1로 부터 setArguments된 데이터를 받아옴
tv_frag2.setText(result);
}
btn_move.setOnClickListener(new View.OnClickListener() { // 프래그먼트 2로 이동
@Override
public void onClick(View view) { // 프래그먼트 1로 이동
Bundle bundle = new Bundle(); // 무언가를 담을 준비를 할 수 있는 보따리 or 꾸러미 같은 느낌
bundle.putString("fromFrag2","khon 프래그먼트 2");
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
Fragment1 fragment1 = new Fragment1();
fragment1.setArguments(bundle);
transaction.replace(R.id.framelayout, fragment1);
transaction.commit(); // 저장
}
});
return view;
}
}