Home > other >  Saving some data in Kotlin fragments
Saving some data in Kotlin fragments

Time:07-06

I have 2 fragments, first(start_fragment) should shows when app starts and have 2 buttons, 1 which close app(declineButton), and second which hide this fragment and show main fragment(webview_fragment). If user press agreeButton, next time app will starts already in main fragment(webview_fragment). How could i save that user press agree button? I tried to create boolean variable but can't imagine how can i put new value from button within fragment to variable within mainactivity.

MainActivity.kt

package com.example.webviewapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle


class MainActivity : AppCompatActivity() {
    var firstlauch: Boolean = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (firstlauch == true) {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.fullcreen_holder, start_fragment())
                .commit()
        } else {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.webview_holder, webview_fragment())
        }
    }
}

start_fragment.kt

package com.example.webviewapp

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button


class start_fragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?

    ): View? {
        val view = inflater.inflate(R.layout.layout_start_fragment, container, false)
        val agreeButton: Button = view.findViewById(R.id.privacy_agree)
        val declineButton: Button = view.findViewById(R.id.privacy_decline)
        agreeButton.setOnClickListener {
            val fragment = webview_fragment()
            val transaction = fragmentManager?.beginTransaction()
            transaction?.remove(this)?.replace(R.id.webview_holder, fragment)?.commit()
        }
        declineButton.setOnClickListener {
            requireActivity().finishAndRemoveTask()
        }
        return view
    }
}

CodePudding user response:

You can use sharedPreferences for keep small data.

In MainActivity

val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
val firstLaunch = sharedPref.getBoolean("YOUR_KEY", true)

In Fragment

val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE) ?: return
val editor = sharedPref.edit()

agreeButton.setOnClickListener {
    editor.putBoolean("YOUR_KEY", true)
    val fragment = webview_fragment()
    val transaction = fragmentManager?.beginTransaction()
    transaction?.remove(this)?.replace(R.id.webview_holder, fragment)?.commit()
}
declineButton.setOnClickListener {
    editor.putBoolean("YOUR_KEY", false)
    requireActivity().finishAndRemoveTask()
}
  • Related