在Kotlin片段中保存一些数据
我有2个片段,首先(start_fragment)应显示应用程序启动何时且有2个按钮,其中1个关闭应用程序(dewbutton)和第二个隐藏此片段并显示主片段(WebView_fragment)。如果用户按CLASSBUTTON,则下一个应用程序将在Main Fragment(WebView_fragment)中启动。如何保存该用户按同意按钮?我试图创建布尔变量,但无法想象如何在片段内的按钮中将新值放在主activity中。
MainAttivity.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
}
}
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 sharedspreferences 保留小数据。
的主要目的
在碎片中
You can use sharedPreferences for keep small data.
In MainActivity
In Fragment