我在 Navigation 组件中使用共享 ViewModel 而不是为每个片段创建一个 ViewModel(主要是因为它更容易)但是现在当我重新输入片段并订阅该片段的 ViewModel 实时数据时出现问题,我得到最后状态也太。
这是 ViewModel 代码:
val apiLessonData: MutableLiveData<String>> = MutableLiveData()
fun getLessonsUserCreated() =
apiCall(MyMaybeObserver(apiLessonData))
Run Code Online (Sandbox Code Playgroud)
在 MyMaybeObserver 中,我有这样的事情:
override fun onSuccess(t: T) {
apiDataObserver.postValue(t)
}
Run Code Online (Sandbox Code Playgroud)
这就是我在片段中观察它的方式:
private val apiAddGoalData = Observer<String> { response ->
showSnack(response)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
.
.
viewModel.apiAddGoalData.observe(viewLifecycleOwner, apiAddGoalData)
.
.
}
Run Code Online (Sandbox Code Playgroud)
现在当我第一次进入它工作正常但我第二次打开它时,它显示了上一次的小吃,如何在不创建新的 ViewModel 的情况下停止它?
android android-fragments android-livedata android-viewmodel
我正在尝试测试我的 recyclerView 并且我正在使用材料卡视图进行项目显示,虽然该应用程序运行良好,但当我尝试测试时出现此错误:
android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
Run Code Online (Sandbox Code Playgroud)
现在测试就这么简单:
@Test
fun shouldShowList() {
launchFragmentInContainer<PostsFragment>()
Thread.sleep(5000)
}
Run Code Online (Sandbox Code Playgroud)
睡眠仅用于应用程序等待尝试显示列表。奇怪的是,当我的列表项布局不使用 materialCardView 时,测试通过了。现在我已将我的应用主题更改为:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
但测试仍然消失,那么如何更改我的应用程序的测试主题?
我想打开另一个应用程序并打开其中的特定页面!在他们的指南中,他们说使用波纹管进行默认操作
android.intent.action.VIEW
Run Code Online (Sandbox Code Playgroud)
并使用一种意图,但没有得到哪种意图,如同下面的特定页面:
somecompany://details?id=com.example.calendar
Run Code Online (Sandbox Code Playgroud)
但我不知道怎么做!我尝试了大量的解决方案,但不能这样做!
谢谢你的帮助.
我可以在不使用意图的情况下将字符串传递给Android中的另一个活动吗?我在意向附加项方面遇到麻烦...它们似乎并不总是有效!那么,还有其他方法吗?
我曾尝试过的意图:
String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");
Run Code Online (Sandbox Code Playgroud)
但是每次我开始活动时,它都会得到两种字符串。第一次与其他时间不同。我可以在不使用意图的情况下将其他活动传递给第二活动吗?