728x90
반응형
번들을 사용하여 프래그먼트 간 데이터를 전달하는 간단한 예제입니다.
activity.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"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
fragment_1.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">
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:hint="이름을 쓰세요"
android:textSize="30sp"/>
<Button
android:id="@+id/btn"
android:layout_width="100dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content"
android:text="확인" />
</LinearLayout>
fragment_2.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">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="이름"
android:textSize="30sp"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//프래그먼트1 연결
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment1 fragment1 = new Fragment1();
transaction.replace(R.id.frame, fragment1);
transaction.commit();
}
}
Fragment1.java
public class Fragment1 extends Fragment {
private View view;
private Button btn;
EditText edit;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_1, container, false);
edit = view.findViewById(R.id.edit);
btn = view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() { // 프래그먼트2로 이동
@Override
public void onClick(View view) {
Bundle bundle = new Bundle(); // 번들을 통해 값 전달
bundle.putString("name",edit.getText().toString());//번들에 넘길 값 저장
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
Fragment2 fragment2 = new Fragment2();//프래그먼트2 선언
fragment2.setArguments(bundle);//번들을 프래그먼트2로 보낼 준비
transaction.replace(R.id.frame, fragment2);
transaction.commit();
}
});
return view;
}
}
Fragment2.java
public class Fragment2 extends Fragment {
private View view;
private TextView tv_name;
private String name;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_2, container, false);
tv_name = view.findViewById(R.id.tv_name);
if (getArguments() != null)
{
name = getArguments().getString("name"); // 프래그먼트1에서 받아온 값 넣기
tv_name.setText(name);
}
return view;
}
}
728x90
반응형
'Mobile > Android' 카테고리의 다른 글
안드로이드 플로팅 버튼 만들기 (0) | 2021.04.30 |
---|---|
안드로이드 캘린더뷰 값 저장하기 (0) | 2021.04.29 |
안드로이드 리사이클러뷰에서 팝업 메뉴 만들기 (0) | 2021.04.22 |
[안드로이드] 리사이클러뷰에서 인텐트로 데이터 전달 (Adapter) (0) | 2021.04.19 |
[안드로이드] 탭호스트로 인텐트 연결하기 (1) | 2021.04.17 |