I'm starting to make an application in Android Studio in Kotlin with fragments and I need, if possible, an example or help on how to make a fragment appear only once, for example, I run an application and a fragment of terms and conditions appears and when I press an accept terms button it advances in the app, but when I start the application again the terms and conditions do not appear again.
you can use shared preferences to store the user selection.
Create a new class called PrefsUtil or name it as per your convenience. inside that convert my java code to kotlin. Androidstudio quickly convert any java file to kotlin.
Then when user clicks on accept terms button, this class will get called and this state will be saved for first launch. Next time when user opens your app, it wont show terms and conditions fragment.
Sharing sample code but its in java. You can convert it to Kotlin.
Hope this helps.
public class PrefsUtil {
private static final String KEY_FIRST_LAUNCH = "is_first_launch";
public static void setQuickSetupComplete(Context context, boolean complete) {
context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE)
.edit()
.putBoolean("quick_setup_complete", complete)
.apply();
}
}
and now call this in your main activity/fragment
I have called it my activity from where I need it to launch only once.
PrefsUtil.setQuickSetupComplete(this, true);