<navigation 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:id="@+id/nav_boarding"
app:startDestination="@id/boardingFragment1">
<fragment
android:id="@+id/boardingFragment1"
android:name="org.sopt.soptandroidseminar.view.fragment.BoardingFragment1"
android:label="첫번째 화면"
tools:layout="@layout/fragment_boarding1">
<action
android:id="@+id/action_boardingFragment1_to_boardingFragment2"
app:destination="@+id/boardingFragment2" />
</fragment>
<fragment
android:id="@+id/boardingFragment2"
android:name="org.sopt.soptandroidseminar.view.fragment.BoardingFragment2"
android:label="두번째 화면"
tools:layout="@layout/fragment_boarding2">
<action
android:id="@+id/action_boardingFragment2_to_boardingFragment3"
app:destination="@+id/boardingFragment3"
app:popUpTo="@id/boardingFragment1"
app:popUpToInclusive="false"/>
</fragment>
<fragment
android:id="@+id/boardingFragment3"
android:name="org.sopt.soptandroidseminar.view.fragment.BoardingFragment3"
android:label="세번째 화면"
tools:layout="@layout/fragment_boarding3" />
</navigation>
네이게이션을 이용한 온보딩 완료
object SOPTSharedPreferences {
private const val STORAGE_KEY = "USER_AUTH"
private const val AUTO_LOGIN = "AUTO_LOGIN"
fun getAutoLogin(context: Context) : Boolean {
val preferences = context.getSharedPreferences(STORAGE_KEY, Context.MODE_PRIVATE)
return preferences.getBoolean(AUTO_LOGIN, false)
}
fun setAutoLogin(context: Context, value: Boolean) {
val preferences = context.getSharedPreferences(STORAGE_KEY, Context.MODE_PRIVATE)
preferences.edit()
.putBoolean(AUTO_LOGIN, value)
.apply()
}
fun setLogout(context: Context) {
val preferences = context.getSharedPreferences(STORAGE_KEY, Context.MODE_PRIVATE)
preferences.edit()
.remove(AUTO_LOGIN)
.clear()
.apply()
}
}
object를 이용한 SharedPreferences 사용 & 자동 로그인 구현
adapter, util, api ,view로 크게 나누어서 폴더링
<action
android:id="@+id/action_boardingFragment2_to_boardingFragment3"
app:destination="@+id/boardingFragment3"
app:popUpTo="@id/boardingFragment1"
app:popUpToInclusive="false"/>
popUpTo와 popUpToInclusive를 사용하여 BackStack 관리
class BoardingActivity: AppCompatActivity() {
private lateinit var binding: ActivityBoardingBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBoardingBinding.inflate(layoutInflater)
setContentView(binding.root)
initToolbar()
}
private fun initToolbar() {
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.container_boarding) as NavHostFragment
val navController = navHostFragment.navController
setSupportActionBar(binding.toolbar)
NavigationUI.setupActionBarWithNavController(this,navController)
val appBarConfiguration = AppBarConfiguration(navController.graph)
binding.toolbar.setupWithNavController(navController, appBarConfiguration)
}
override fun onSupportNavigateUp() = findNavController(R.id.container_boarding).navigateUp()
}
navigation의 label과 Toolbar를 연동하여 title의 내용을 구현했습니다.
- 배운 내용 : navigation을 활용한 온보딩에 대해 배울 수 있었습니다.