簡體   English   中英

如何在Android Conductor中保留方向更改的視圖狀態?

[英]How to retain state of views of orientation changes in Android Conductor?

我目前正在學習Android的Conductor框架,但對它的工作方式有一些問題或誤解。

我的印象是該方法

setRetainViewMode(RetainViewMode.RETAIN_DETACH);

將視圖的狀態保存在控制器中。 為了測試行為,我添加了EditText視圖,在其中輸入了一個值並旋轉了屏幕。 我還添加了2個附加了onclick偵聽器的視圖,更改了onclick的背景顏色

測試的結果是EditText視圖保留了狀態並保留了輸入的值。 但是,這兩個視圖已變回其原始背景色(無)。

無論設置哪個RetainViewMode,這都是視圖的行為

我有這個簡單的MainActivity(注意:我是用Kotlin編寫的):

    class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var router: Router = Conductor.attachRouter(this, controller_container, savedInstanceState)
        if (!router.hasRootController()) {
            var t : TestController = TestController()
            t.retainViewMode = Controller.RetainViewMode.RETAIN_DETACH
            router.setRoot(RouterTransaction.with(t))
        }
    }

    companion object doTask {
        fun start(activity : Activity) {
            val intent = Intent(activity, MainActivity::class.java)
            activity.startActivity(intent)
        }
    }
}

這是TestController:

    class TestController : BaseController() {

    var i : Int = 0
    var h : Int = 0

    override fun onViewBound(view: View) {
        view.a.setOnClickListener {
            i++
            if (i % 2 == 0) {
                view.a.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.white))
            } else {
                view.a.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.turtle_green))
            }
        }

        view.b.setOnClickListener {
            h++
            if (h % 2 == 0) {
                view.b.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.white))
            } else {
                view.b.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.blue_light))
            }
        }
    }

    override fun inflateView(inflater: LayoutInflater, container: ViewGroup): View {
        return inflater.inflate(R.layout.controller_layout_test, container, false)
    }

}

和xml布局文件controller_layout_test:

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

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/logo_simple"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="30dp"
            android:layout_gravity="center"
            android:background="@color/transparent50p"
            android:padding="20dp">

            <EditText
                android:id="@+id/gt"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:padding="6dp"
                android:background="@color/white_transparent50p"/>

                />

            <EditText
                android:id="@+id/erergeargf"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:padding="6dp"
                android:background="@color/white_transparent50p"/>

            <View
                android:id="@+id/a"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="5dp"
                android:layout_gravity="center"></View>

            <View
                android:id="@+id/b"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_margin="5dp"
                android:layout_gravity="center"></View>
        </LinearLayout>

    </ScrollView>

</FrameLayout>

activity_main xml布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="dk.minreklame.minetilbud_v2.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        />

</android.support.design.widget.AppBarLayout>

<com.bluelinelabs.conductor.ChangeHandlerFrameLayout
    android:id="@+id/controller_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />

</android.support.design.widget.CoordinatorLayout>

由於您的視圖引用了宿主活動,因此它們永遠不會在方向更改中保留。 那會導致內存泄漏。 有關RETAIN_DETACH狀態的文檔:

分離時,Controller將保留其對視圖的引用,但是在發生配置更改時,控制器仍將釋放該引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM