27.1.x中的ViewModel功能因子活动的配置更改而中断

时间:2018-08-13 12:00:17

标签: java android android-lifecycle android-orientation android-viewmodel

我遇到了这种奇怪的行为,并认为它可能引起社区的兴趣。如果您为父级活动定义了ViewModel,然后启动子级活动,进行方向更改(我使用的是配置有API = 24的仿真器),然后返回到父级活动,则父级的ViewModel被销毁,并且全新的是通过ViewModelProviders.of方法分配给您的。显然这不是应该发生的事情。我可能会误用此功能吗?

下面是重新创建问题的示例代码。主要活动显示指向ViewModel的指针。按下按钮开始子活动,然后进行方向更改,然后按返回以返回到父活动。它将更新ViewModel指针。另一方面,如果您从父活动更改方向,则不会破坏ViewModel。同样,如果您对子活动进行了方向更改,然后再进行另一次操作来撤消它,则父活动将保留原始的ViewModel。走吧。

MainActivity.java

package com.google.example.viewmodeltest;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyViewModel viewModel=ViewModelProviders.of(this).get(MyViewModel.class);
        ((TextView) findViewById(R.id.infoText)).setText("ViewModel="+viewModel);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,ChildActivity.class));
            }
        });
    }
}

ChildActivity.java

package com.google.example.viewmodeltest;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

public class ChildActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_child);
    }
}

MyViewModel.java

package com.google.example.viewmodeltest;

import android.arch.lifecycle.ViewModel;

public class MyViewModel extends ViewModel {
    public MyViewModel() {
        super();
    }
}

layout / activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView android:id="@+id/infoText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Press"/>
</LinearLayout>

layout / activity_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the child activity"/>

</LinearLayout>

模块build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.google.example.viewmodeltest"
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
//  def supportlib_version = "28.0.0-rc01"
    def supportlib_version = "27.1.1"

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "com.android.support:appcompat-v7:$supportlib_version"
    implementation "com.android.support:design:$supportlib_version"
    implementation "com.android.support.constraint:constraint-layout:1.1.2"
    implementation "android.arch.lifecycle:extensions:1.1.1"
    annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.example.viewmodeltest">

    <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=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ChildActivity" />
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

经过反复试验,我确定此“功能”存在于支持库的27.1.x版本中,但在27.0.x和28.0.0上均不存在。希望这次它保持不变。