C# 将新字符串从MainActivity类注入片段/视图模型
使用android studio底部导航活动模板,希望每次从底部导航访问主页时,我的C# 将新字符串从MainActivity类注入片段/视图模型,c#,android,kotlin,android-fragments,bottomnavigationview,C#,Android,Kotlin,Android Fragments,Bottomnavigationview,使用android studio底部导航活动模板,希望每次从底部导航访问主页时,我的HomeFragment/HomeViewModel页面文本都发生更改。当我第一次单击它时,它显示的文本应该与第二次显示的文本不同 我能够创建一个保存文本输出的变量,并且我已经创建了一个函数,该函数应该能够更新该变量,但是我无法在MainActivity类中调用它。有什么想法吗 main活动 class MainActivity : AppCompatActivity() { override fun o
HomeFragment
/HomeViewModel
页面文本都发生更改。当我第一次单击它时,它显示的文本应该与第二次显示的文本不同
我能够创建一个保存文本输出的变量,并且我已经创建了一个函数,该函数应该能够更新该变量,但是我无法在MainActivity
类中调用它。有什么想法吗
main活动
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications))
navView.setupWithNavController(navController)
}
HomeFragment
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
var chosenOutput:String = "primary output";
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
//using update here crashes the app
homeViewModel =
ViewModelProvider(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = root.findViewById(R.id.text_home)
homeViewModel.text.observe(viewLifecycleOwner, Observer {
textView.text = chosenOutput; //changing 'chosenOutput' to 'it' means that text update must take place within HomeViewModel instead of fragment.
})
return root
}
fun update(result: String)
{
chosenOutput = result;
}
}
HomeViewModel
class HomeViewModel : ViewModel() {
var temp:String = "demo"
//constructor()
private val _text = MutableLiveData<String>().apply {
value = temp;
}
fun update(result: String) //function may be obsolete if text change can be done in fragment.
{
temp = result;
}
val text: LiveData<String> = _text
}
class HomeViewModel:ViewModel(){
var temp:String=“演示”
//构造函数()
private val_text=MutableLiveData()。应用{
数值=温度;
}
fun update(result:String)//若文本更改可以在片段中完成,则函数可能已过时。
{
温度=结果;
}
val text:LiveData=\u text
}
将字符串从我给定的
MainActivity
类注入Fragment
或ViewModel
的正确方法是什么?您可以更改方法并将chosenOutput
变量存储在MainActivity
每次选择HomeFragment
class MainActivity : AppCompatActivity()
{
var chosenOutput = "primary output"
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications))
navView.apply {
setupWithNavController(navController)
setOnNavigationItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.navigation_home -> {
chosenOutput = "new value" // <-- Update chosen output here
// Add this method to call the `onResume` method in the fragment
showFragment(HomeFragment.newInstance())
}
else { }
}
true
}
}
private fun showFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.nav_host_fragment, fragment)
.addToBackStack(null)
.commit()
}
}
使用sharedviewmodel切换到“活动”,使用livedata设置值并观察相同值。似乎不起作用,仍然输出“主要输出”。在这之后,我尝试将
val newChosenOutput=(活动作为MainActivity)。chosenOutput
更改为chosenOutput=(活动作为MainActivity)。chosenOutput
,但这也不起作用,似乎没有达到onResume()?我编辑了答案并添加了代码,以便在选择导航项时以编程方式设置当前片段。现在,它应该到达onResume
方法。@luce_999似乎没有menuframent
的引用,我无法返回它或创建newInstance
作为它的一个实例。很抱歉我拼错了,我指的是HomeFragment
class HomeFragment : Fragment()
{
companion object
{
fun newInstance() : HomeFragment
{
return HomeFragment()
}
}
override fun onResume() {
super.onResume()
val newChosenOutput = (activity as MainActivity).chosenOutput
}
}