Issue
This is more of an advice thread so I can't share any code because I have none.
My question is the following:
I have an android app that uses a nav graph and if I leave the app with the system back button it will always open the login screen on resume. So I didn't close the app, I just put it into the background. My start destination is indeed set to this login fragment, but what must I change that the app would open on the screen I close the app on? In this case, it was the screen after login which in my case can point to 2 different fragments, so I can't just hardcode it.
Solution
Yes, you can do it using SharedPreference.
- In the login fragment make a shared preference and set its value to a boolean.
val sharedPreferences:SharedPreferences = this.requireActivity().getSharedPreferences("OnboardingDetails", Context.MODE_PRIVATE)
val boarded: Boolean = sharedPreferences.getBoolean("isLogin", false)
if (boarded) {
findNavController().navigate(OnboardingFragmentDirections.actionOnboardingFragmentToTransactionListFragment())
} else {
sharedPreferences.edit().putBoolean("isLogin", true).apply()
}
- The first time the login screen comes when a user installs the app, the boolean is false.
- else part of the condition executes making the boolean true. This means the user has already gone through the login screen.
- When the next time user opens your app, The boolean is true and if part of the condition runs that takes the user to the next fragment automatically.
Answered By - Karmveer Singh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.