Navigation – 모던 안드로이드 (오준석의 생존코딩)
Navigation – 모던 안드로이드 (오준석의 생존코딩)
https://www.youtube.com/watch?v=MHRt52NNzh0&list=PLxTmPHxRH3VXHOBnaGQcbSGslbAjr8obc&index=11
1. MainFragment 생성 (버튼 한개 생성)
2. SecondFragment 생성 (텍스트뷰 한개 생성)
3. res 폴더에서 navigation Directory 생성 (Resource type : navigation)
4. navigation 폴더에서 Navigation Resource File 생성 (파일명 : nav_graph.xml)
5. + 메뉴를 클릭하고 MainFragment 와 SecondFragment 를 화면에 배치
6. MainFragment 에서 SecondFragment 로 화살표로 이어줌 (액션 추가)
7. MainActivity 레이아웃에서 NavHostFragment 추가
8. MainFragment 에 있는 버튼을 눌렀을 때 SecondFragment 로 이동하기
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_secondFragment2);
}
});
}
https://developer.android.com/guide/navigation/navigation-navigate#java
9. nav_graph.xml 에서 MainFragment 와 SecondFragment 의 Label 수정 (툴바에 표시되는 제목)
10. Action Bar 와 연결
public class MainActivity extends AppCompatActivity {
AppBarConfiguration appBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); // activity_main.xml 의 NavHostFragment 의 id
appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
}
https://developer.android.com/guide/navigation/navigation-ui#action_bar
11. 프레그먼트 간 데이터 전달 (Safe Args 사용)
# nav_graph.xml 에서 SecondFragment 에서 Argument 에서 + 누루기
name : var
type : String
# MainFragment 에서 데이터 전달
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final MainFragmentDirections.ActionMainFragmentToSecondFragment2 action =
MainFragmentDirections.actionMainFragmentToSecondFragment2("Hello!!");
view.findViewById(R.id.button).setOnClickListener(view1 -> {
Navigation.findNavController(view1).navigate(action);
});
}
# SecondFragmetn 에서 데이터 수신
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String var = SecondFragmentArgs.fromBundle(getArguments()).getVar();
TextView textView = view.findViewById(R.id.textView);
textView.setText(var);
}
# top 수준 build.gradle
buildscript { repositories { google() } dependencies { def nav_version = "2.3.1" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } }
# app 수준 build.gradle
apply plugin: "androidx.navigation.safeargs"
https://developer.android.com/guide/navigation/navigation-pass-data#Safe-args
dependencies 추가
https://developer.android.com/jetpack/androidx/releases/navigation#safe_args
12. 뒤로 가기 버튼을 눌렀을 때 오류가 난다면 추가
@Override
public void onBackPressed() {
super.onBackPressed();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.navigateUp(navController, appBarConfiguration);
}