Java 在片段中前后导航时,如何将输入的数据保存在片段中
我有三个片段:Java 在片段中前后导航时,如何将输入的数据保存在片段中,java,android,android-fragments,Java,Android,Android Fragments,我有三个片段:FragmentA,FragmentB,FragmentC。所有片段都有一个TextInputLayout,里面有editText,并有按钮转到下一个片段。调用FragmentA时,我在editText中输入文本,然后转到下一个片段(FragmentB),并对最后一个片段执行相同的操作和相同的操作(FragmentC)。如果我返回到FragmentB并在editText中键入不同的内容,然后转到FragmentC文本,则三个文本已消失。我知道我必须将bundle传递给fragmen
FragmentA
,FragmentB
,FragmentC
。所有片段都有一个TextInputLayout
,里面有editText
,并有按钮转到下一个片段。调用FragmentA
时,我在editText
中输入文本,然后转到下一个片段(FragmentB
),并对最后一个片段执行相同的操作和相同的操作(FragmentC
)。如果我返回到FragmentB
并在editText
中键入不同的内容,然后转到FragmentC
文本,则三个文本已消失。我知道我必须将bundle传递给fragment,当使用onDetach()
方法在手机上刷回时将显示该片段。我知道如果我想将bundle传递给我将通过按下next按钮调用的片段,我必须setArguments(bundle)
我正在使用transaction.add()
和transaction.hide()
,因为如果我使用transaction,当我从上一个片段返回时,我丢失了在editText
中输入的文本。replace()要在片段之间存储和保留值,请查看SharedReferences
。使用此选项在片段之间传递值
holder.YOURBUTTONCLICKID.setOnClickListener {
val sharedPrefs = context
.getSharedPreferences("PREFS", Context.MODE_PRIVATE)
sharedPrefs.edit().putString("postId", EDITTEXTID).apply()
(context as FragmentActivity)
.supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, FRAGMENTA/B/C )
.addToBackStack(null).commit()
}
用相关ID替换大写字母
得到另一个片段的值,就像这样
val preference = context?.getSharedPreferences("PREFS", Context.MODE_PRIVATE)
if (preference != null) {
getTextValue = preference.getString("postId", "none").toString()
}
请记住,您的putString
键和getString
必须是相同的要在片段之间存储和保留值,请查看SharedReferences
。使用此选项在片段之间传递值
holder.YOURBUTTONCLICKID.setOnClickListener {
val sharedPrefs = context
.getSharedPreferences("PREFS", Context.MODE_PRIVATE)
sharedPrefs.edit().putString("postId", EDITTEXTID).apply()
(context as FragmentActivity)
.supportFragmentManager.beginTransaction()
.replace(R.id.fragment_container, FRAGMENTA/B/C )
.addToBackStack(null).commit()
}
用相关ID替换大写字母
得到另一个片段的值,就像这样
val preference = context?.getSharedPreferences("PREFS", Context.MODE_PRIVATE)
if (preference != null) {
getTextValue = preference.getString("postId", "none").toString()
}
请记住,您的putString
键和getString
必须是相同的