Android 当我回到一个片段时,观察者立即被调用
我有一个观察器,它的名字叫“变化碎片” 问题是,当我返回时,会立即调用observer,我的应用程序会因错误而崩溃 java.lang.IllegalArgumentException:导航目的地 com.superapps.ricardo.tablepro:id/action\u searchFragment\u to\u yourGameList2 此导航控制器未知 我不明白为什么叫它 这是更改列表的唯一方法Android 当我回到一个片段时,观察者立即被调用,android,kotlin,observers,Android,Kotlin,Observers,我有一个观察器,它的名字叫“变化碎片” 问题是,当我返回时,会立即调用observer,我的应用程序会因错误而崩溃 java.lang.IllegalArgumentException:导航目的地 com.superapps.ricardo.tablepro:id/action\u searchFragment\u to\u yourGameList2 此导航控制器未知 我不明白为什么叫它 这是更改列表的唯一方法 override fun onSuccess(gamePair: Pair<
override fun onSuccess(gamePair: Pair<Int, List<BggGame>>) {
CoroutineScope(Main).launch{
//goToList(gamePair.second, binding.input.text.toString())
viewModel.setGameList(gamePair.second)
}
}
成功时覆盖乐趣(游戏对:对){
协同观测仪(主)。发射{
//goToList(gamePair.second,binding.input.text.toString())
viewModel.setGameList(gamePair.second)
}
}
这是viewmodel创建和更改片段代码
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(SearchViewModel::class.java)
viewModel.gameList.observe(viewLifecycleOwner, Observer {
goToList(it, binding.input.text.toString())
})
}
private fun goToList(games: List<BggGame>, user: String) {
val action = SearchFragmentDirections.actionSearchFragmentToYourGameList2(user)
val gameList = GameList()
gameList.gameList = games
action.gameList = gameList
try {
Navigation.findNavController(view!!).navigate(action)
viewModel.gameList.removeObservers(viewLifecycleOwner)
} catch (e: Exception){
Log.e("a0,","a..", e)
}
progressDialog.dismiss()
}
覆盖活动创建的乐趣(savedInstanceState:Bundle?){
super.onActivityCreated(savedInstanceState)
viewModel=ViewModelProviders.of(this.get)(SearchViewModel::class.java)
viewModel.gameList.observe(viewLifecycleOwner,Observer{
goToList(it,binding.input.text.toString())
})
}
私人娱乐哥特式玩家(游戏:列表,用户:字符串){
val action=SearchFragmentDirections.actionSearchFragmentToYourGameList2(用户)
val gameList=gameList()
gameList.gameList=游戏
action.gameList=游戏列表
试一试{
导航。findNavController(视图!!)。导航(操作)
viewModel.gameList.removeObservers(viewLifecycleOwner)
}捕获(e:例外){
Log.e(“a0,,“a…”,e)
}
progressDialog.disclose()的
}
LiveData
保留上次设置的值。在LivaData
上调用observe()
时,如果LiveData
有一个值,将立即使用先前设置的值调用观察者
如果您想将LiveData
用于像您的用例这样的“事件”,那么您的实时数据应该公开一个只能使用一次的Event
对象
下面是此类事件
类的一个好例子
从文章中:
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
LiveData
保留上次设置的值。在LivaData
上调用observe()
时,如果LiveData
有一个值,将立即使用先前设置的值调用观察者
如果您想将LiveData
用于像您的用例这样的“事件”,那么您的实时数据应该公开一个只能使用一次的Event
对象
下面是此类事件
类的一个好例子
从文章中:
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
在
viewModel
中,使用SingleLiveEvent
而不是MutableLiveData
或LiveData
这是SingleLiveEvent
类,您可以在util
包中使用它:
import android.util.Log;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import java.util.concurrent.atomic.AtomicBoolean;
public class SingleLiveEvent<T> extends MutableLiveData<T> {
private static final String TAG = "SingleLiveEvent";
private final AtomicBoolean mPending = new AtomicBoolean(false);
@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
if (hasActiveObservers()) {
Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
}
// Observe the internal MutableLiveData
super.observe(owner, new Observer<T>() {
@Override
public void onChanged(@Nullable T t) {
if (mPending.compareAndSet(true, false)) {
observer.onChanged(t);
}
}
});
}
@MainThread
public void setValue(@Nullable T t) {
mPending.set(true);
super.setValue(t);
}
}
导入android.util.Log;
导入androidx.annotation.MainThread;
导入androidx.annotation.NonNull;
导入androidx.annotation.Nullable;
导入androidx.lifecycle.LifecycleOwner;
导入androidx.lifecycle.MutableLiveData;
导入androidx.lifecycle.Observer;
导入java.util.concurrent.AtomicBoolean;
公共类SingleLiveEvent扩展了MutableLiveData{
私有静态最终字符串TAG=“SingleLiveEvent”;
private final AtomicBoolean mPending=新的AtomicBoolean(false);
@凌驾
public void observe(@NonNull LifecycleOwner owner,@NonNull observe在viewModel
中使用SingleLiveEvent
而不是MutableLiveData
或LiveData
这是SingleLiveEvent
类,您可以在util
包中使用它:
import android.util.Log;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import java.util.concurrent.atomic.AtomicBoolean;
public class SingleLiveEvent<T> extends MutableLiveData<T> {
private static final String TAG = "SingleLiveEvent";
private final AtomicBoolean mPending = new AtomicBoolean(false);
@Override
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {
if (hasActiveObservers()) {
Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
}
// Observe the internal MutableLiveData
super.observe(owner, new Observer<T>() {
@Override
public void onChanged(@Nullable T t) {
if (mPending.compareAndSet(true, false)) {
observer.onChanged(t);
}
}
});
}
@MainThread
public void setValue(@Nullable T t) {
mPending.set(true);
super.setValue(t);
}
}
导入android.util.Log;
导入androidx.annotation.MainThread;
导入androidx.annotation.NonNull;
导入androidx.annotation.Nullable;
导入androidx.lifecycle.LifecycleOwner;
导入androidx.lifecycle.MutableLiveData;
导入androidx.lifecycle.Observer;
导入java.util.concurrent.AtomicBoolean;
公共类SingleLiveEvent扩展了MutableLiveData{
私有静态最终字符串TAG=“SingleLiveEvent”;
private final AtomicBoolean mPending=新的AtomicBoolean(false);
@凌驾
公共空间观察(@NonNull LifecycleOwner owner,@NonNull Observer您忘记了示例XD确实,我用一个链接更新了帖子,该链接指向问题的一个很好的解释。您忘记了示例XD确实,我用一个链接更新了帖子,该链接指向问题的一个很好的解释。请共享您的viewModel代码!!请共享您的viewModel代码!!