4

我有一个非常简单的应用程序,其中包含一个虚拟Activity和虚拟的 Android Lifecycle ViewModel ViewModel

片段活动

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()
    }
}

视图模型工厂

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 的步骤

  1. 将应用设置为横向模式
  2. 锁定屏幕
  3. 解锁屏幕并观察 ViewModel 消失了,Activity 被销毁并创建了两次。

日志输出

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

此行为与此处的发现一致,但是,我希望 arch 库能够处理这种情况,因为它非常标准,并且纵向模式下的锁定和解锁按预期工作。

在这种特殊情况下防止它ViewModel被破坏有什么好主意吗?

在 Nexus 5X、API 27 上测试

编辑 1:在添加要保存的字符串onSaveInstanceState并检查该字符串是否在所有活动破坏和创建中持续存在之后,我很确定这是库的错误。

编辑2:为什么这是一个问题?

问题 1:在横向锁定的情况下,捆绑包以某种方式从 Activity 244798673 正确路由到 Activity 218111434 02-20 16:30:33.241,但是ViewModel无法通过这一系列操作持续存在。这与捆绑行为不一致,因为我们在技术上仍然在同一个 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在纵向锁定和解锁中持续存在,这与横向场景不一致。

4

2 回答 2

1

这实际上是一个源自 Android 框架的 bug:

https://issuetracker.google.com/issues/73644080

底层的 Android Arch 库使用保留的片段来持久化ViewModels。实验上,在打开 PIN / PATTERN / SWIPE / PASSWORD 的情况下,在横向模式下锁定和解锁设备的相同场景中,保留的碎片也将无法生存。因此,每次我们解锁设备时,我们都会获得一个新的ViewModel.

一些用例:

  1. 设备:NEXUS 5X,API 级别:24,锁定方式:PIN / PATTERN / SWIPE / PASSWORD。可以通过任何解锁方法解锁来重现。也可以通过指纹解锁来重现。

  2. 设备:NEXUS 5X,API 级别:27,锁定方式:PIN / PATTERN / PASSWORD 。只有在使用指纹解锁设备时才能重现。使用 PIN / PATTERN / PASSWORD 解锁效果很好。

  3. 设备:Pixel,API等级:27,锁定方式:PIN(其他未测试)+指纹。只有在使用指纹解锁设备时才能重现。使用 PIN / PATTERN / PASSWORD 解锁效果很好。

于 2018-03-14T18:32:46.263 回答
0

您在此处提供的 ViewModelFactory 不是单例。这应该是问题所在。

val myViewModel: MyViewModel = ViewModelProviders
                .of(this, VmFactory())
                .get(MyViewModel::class.java)

将工厂设为单例,它应该可以工作。

于 2018-03-11T18:57:02.503 回答