我有一个非常简单的应用程序,带有虚拟Activity
和虚拟Android生命周期ViewModel ViewModel
。
FragmentActivity
class FragmentActivity: AppCompatActivity() {
companion object {
private const val TAG = "FragmentActivity"
private const val KEY = "key_key"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_fragment)
Log.d(TAG, "Activity ${hashCode()}, onCreate: orientation ${resources.configuration.orientation}")
if (savedInstanceState != null) {
Log.d(TAG, "Activity ${hashCode()}, onCreate: saved string from savedInstanceState ${savedInstanceState.getString(KEY)}")
} else {
Log.d(TAG, "Activity ${hashCode()}, onCreate: no savedInstanceState")
}
val myViewModel: MyViewModel = ViewModelProviders
.of(this, VmFactory())
.get(MyViewModel::class.java)
}
override fun onResume() {
super.onResume()
Log.d(TAG, "Activity ${hashCode()}, onResume: orientation ${resources.configuration.orientation}")
}
override fun onStop() {
super.onStop()
Log.d(TAG, "Activity ${hashCode()}, onStop: orientation ${resources.configuration.orientation}")
}
override fun onDestroy() {
super.onDestroy()
Log.d(TAG, "Activity ${hashCode()}, onDestroy: orientation ${resources.configuration.orientation}")
}
override fun onSaveInstanceState(outState: Bundle?) {
val savedString = "SAVED_STATE_" + hashCode()
outState?.putString(KEY, savedString)
Log.d(TAG, "Activity ${hashCode()}, onSaveInstanceState: $savedString")
super.onSaveInstanceState(outState)
}
}
视图模型
class MyViewModel: ViewModel() {
companion object {
private const val TAG = "MyViewModel"
}
init {
Log.d(TAG, "MyViewModel ${hashCode()}: created")
}
override fun onCleared() {
Log.d(TAG, "MyViewModel ${hashCode()}: onCleared")
super.onCleared()
}
}
ViewModelFactory
class VmFactory: ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass == MyViewModel::class.java) {
return MyViewModel() as T
} else {
throw IllegalArgumentException()
}
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dkarmazi.unknownmemorysampleapp">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".WebViewActivity">
</activity>
<activity android:name=".FragmentActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
杀死ViewModel的步骤
记录输出
02-20 16:30:14.159 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 244798673, onCreate: orientation 2
02-20 16:30:14.159 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 244798673, onCreate: no savedInstanceState
02-20 16:30:14.169 8296-8296/com.dkarmazi.viewmodelscoping D/MyViewModel: MyViewModel 55090662: created
02-20 16:30:14.183 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 244798673, onResume: orientation 2
### LOCKED IN LANDSCAPE MODE
02-20 16:30:22.978 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 244798673, onSaveInstanceState: SAVED_STATE_244798673
02-20 16:30:22.996 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 244798673, onStop: orientation 2
### UNLOCKED IN LANDSCAPE MODE
02-20 16:30:33.177 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 244798673, onStop: orientation 2
02-20 16:30:33.178 8296-8296/com.dkarmazi.viewmodelscoping D/MyViewModel: MyViewModel 55090662: onCleared
02-20 16:30:33.179 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 244798673, onDestroy: orientation 2
02-20 16:30:33.241 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 218111434, onCreate: orientation 1
02-20 16:30:33.241 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 218111434, onCreate: saved string from savedInstanceState SAVED_STATE_244798673
02-20 16:30:33.242 8296-8296/com.dkarmazi.viewmodelscoping D/MyViewModel: MyViewModel 113479034: created
02-20 16:30:33.248 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 218111434, onResume: orientation 1
02-20 16:30:33.705 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 218111434, onSaveInstanceState: SAVED_STATE_218111434
02-20 16:30:33.710 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 218111434, onStop: orientation 1
02-20 16:30:33.712 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 218111434, onDestroy: orientation 1
02-20 16:30:33.815 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 158140230, onCreate: orientation 2
02-20 16:30:33.815 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 158140230, onCreate: saved string from savedInstanceState SAVED_STATE_218111434
02-20 16:30:33.822 8296-8296/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 158140230, onResume: orientation 2
此行为与调查结果here一致,但是,我希望拱库能够处理这种情况,因为它非常标准,并且纵向模式下的锁定和解锁按预期工作。
有关阻止ViewModel
在此特定情况下被销毁的任何好主意吗?
在Nexus 5X,API 27上测试
编辑1:在添加要保存在onSaveInstanceState
中的字符串并检查该字符串是否在所有活动中持续销毁并创建后,我很确定这是一个错误图书馆。
编辑2:为什么这是一个问题?
问题1:在横向锁定的情况下,捆绑包以某种方式正确地从活动244798673路由到02-20 16:30:33.241
的活动218111434,但是ViewModel
无法通过这一系列操作继续存在。这与bundle行为不一致,因为我们在技术上仍然在同一个Activity范围内。
问题2:以纵向模式锁定和解锁的日志输出:
02-20 16:38:10.283 8567-8567/? D/FragmentActivity: Activity 244798673, onCreate: orientation 1
02-20 16:38:10.283 8567-8567/? D/FragmentActivity: Activity 244798673, onCreate: no savedInstanceState
02-20 16:38:10.293 8567-8567/? D/MyViewModel: MyViewModel 55090662: created
02-20 16:38:10.301 8567-8567/? D/FragmentActivity: Activity 244798673, onResume: orientation 1
02-20 16:38:13.459 8567-8567/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 244798673, onSaveInstanceState: SAVED_STATE_244798673
02-20 16:38:13.480 8567-8567/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 244798673, onStop: orientation 1
02-20 16:38:17.704 8567-8567/com.dkarmazi.viewmodelscoping D/FragmentActivity: Activity 244798673, onResume: orientation 1
ViewModel
持续存在于纵向锁定和解锁状态,这与横向场景不一致。
答案 0 :(得分:0)
您在此处提供的ViewModelFactory不是Singleton。这应该是问题。
val myViewModel: MyViewModel = ViewModelProviders
.of(this, VmFactory())
.get(MyViewModel::class.java)
让工厂变成单身,它应该有用。
答案 1 :(得分:0)
这实际上是一个源自Android框架的错误:
https://issuetracker.google.com/issues/73644080
引擎盖下的Android Arch Library使用保留的片段来保留ViewModels
。实验上,在打开PIN / PATTERN / SWIPE / PASSWORD时,在横向模式下锁定和解锁设备的情况相同,保留的碎片也无法生存。因此,每次我们解锁设备时,我们都会获得ViewModel
的新实例。
一些用例:
设备:NEXUS 5X,API级别:24,锁定方式:PIN / PATTERN / SWIPE / PASSWORD。可以通过任何解锁方法解锁来重现。也可以通过指纹解锁来重现。
设备:NEXUS 5X,API级别:27,锁定方式:PIN / PATTERN / PASSWORD。只有在使用指纹解锁设备时才能重现。使用PIN / PATTERN / PASSWORD解锁工作正常。
设备:Pixel,API级别:27,锁定方式:PIN(未测试其他)+指纹。只有在使用指纹解锁设备时才能重现。使用PIN / PATTERN / PASSWORD解锁工作正常。