일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- GithubActions
- GDGCampusKorea
- AWS
- kubecost설치
- homelab
- eks비용
- GDSC
- kubecost
- 쿠버네티스
- karpenter
- 커뮤니티
- Python
- GDG
- ci/cd
- Whatever
- 나동빈
- DFS
- kubecostinstall
- 프로그래머스
- 설치
- ekskubecost
- AwsCloudClubs
- eks
- CUK
- 파이썬
- BFS
- k8s
- CI
- 비용최적화
- 코테
Archives
- Today
- Total
촬리의늘솔길
앱만들기.39(Fragment 간 데이터 전송) 본문
FrameLayout 은 프래그먼트들이 체인지 될때 화면 공간
Fragment 에서 아이디 불러올때는 view사용해줘얗
tv_frag1 = view.findViewById(R.id.tv_frag1
- 프래그먼트 트랜잭션 (Fragment Transaction) 트랜잭션(Transaction) 이란, 어떤 대상에 대해 추가, 제거, 변경 등의 작업들이 발생하는것을 묶어서 얘기하는 것입니다. 프래그먼트 매니저는 액티비티가 사용자의 입력 이벤트에 따라 프래그먼트를 추가 및 삭제 그리고 교체 등의 작업들을 수행 할 수 있게 해줍니다. 뿐만아니라 행해진 트랜잭션의 상태를 프래그먼트 백스택(Fragment Backstack) 을 통해 저장할 수 있습니다.
출처: https://tedrepository.tistory.com/6 [Ted's IT Repository]
Bundle의 사전적 의미.
- 꾸러미, 묶음, 보따리
Bundle은 여러가지의 타입의 값을 저장하는 Map 클래스이다.
예를 들면 string 값을 Bundle 클래스에 Mapping(대응, 변환)하는 것이다.
Android에서는 Activity간에 데이터를 주고 받을 때 Bundle 클래스를 사용하여 데이터를 전송한다.
프래그먼트는 액티비티가 품고있는자식이기 때문에 getActivity라는 구문이 가능하다.
MainActivity에서는 할필요없음
최초 앱 실행시 프래그먼트 1 뜰수있게 메인에 추가해주기
Main.java
package com.example.fragmentexample;
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();
//Intent와 비슷한것
Fragment1 fragment1= new Fragment1();
transaction.replace(R.id.framelayout,fragment1);
transaction.commit(); // 저장
}
}
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>
Frag1.java
package com.example.fragmentexample;
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 {
@Nullable
private View view;
private TextView tv_frag1;
private Button btn_move;
private String result;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//프래그먼트 처음 수행될때 생명주기
view = inflater.inflate(R.layout.fragment1,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() {
@Override
public void onClick(View v) { //프래그먼트 2로 이동
Bundle bundle = new Bundle();//무언가를 담을 준비할 수 있 는보따리 or 꾸러미
bundle.putString("FromFrag1","프래그먼트 1");
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
//Intent와 비슷한것
Fragment2 fragment2= new Fragment2();
fragment2.setArguments(bundle);
transaction.replace(R.id.framelayout,fragment2);
transaction.commit(); // 저장
}
});
return view;
}
}
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:layout_marginEnd="16dp"
android:text="프래그먼트1"
android:textSize="60sp"
app:layout_constraintEnd_toEndOf="parent"
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_frag2"
app:layout_constraintStart_toStartOf="@+id/tv_frag2"
app:layout_constraintTop_toBottomOf="@+id/tv_frag2" />
</androidx.constraintlayout.widget.ConstraintLayout>
Frag2.java
package com.example.fragmentexample;
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 {
@Nullable
private View view;
private TextView tv_frag2;
private Button btn_move;
private String result;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//프래그먼트 처음 수행될때 생명주기
view = inflater.inflate(R.layout.fragment2,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() {
@Override
public void onClick(View v) { //프래그먼트 2로 이동
Bundle bundle = new Bundle();//무언가를 담을 준비할 수 있 는보따리 or 꾸러미
bundle.putString("FromFrag2","프래그먼트 2");
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
//Intent와 비슷한것
Fragment1 fragment1= new Fragment1();
fragment1.setArguments(bundle);
transaction.replace(R.id.framelayout,fragment1);
transaction.commit(); // 저장
}
});
return view;
}
}
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:layout_marginEnd="16dp"
android:text="프래그먼트2"
android:textSize="60sp"
app:layout_constraintEnd_toEndOf="parent"
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>
728x90
'✍~2022 > app(android studio)' 카테고리의 다른 글
aws ec2 + rds + node.js +mysql (0) | 2022.07.31 |
---|---|
앱만들기.41(CardView) (0) | 2021.08.18 |
앱만들기.38(Table Layout) (0) | 2021.08.18 |
앱만들기.37(Google Login) (0) | 2021.08.18 |
앱만들기.36(Check Box) (0) | 2021.08.16 |