在方向更改时保持动态设置的View标签 [英] Maintaining dynamically set View tag on orientation change

查看:66
本文介绍了在方向更改时保持动态设置的View标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 setTag()动态设置 View 的标签时,在更改方向后不会保留该标签.几个相关问题的答案(尤其是这两个答案-和 onRestoreInstanceState 来保存需要保存的任何数据活动被销毁并重新创建后会持续存在.

  @Override公共无效onSaveInstanceState(Bundle outState){super.onSaveInstanceState(savedInstanceState);savedInstanceState.putString("tag",消息);}@Overridepublic void onRestoreInstanceState(Bundle savedInstanceState){super.onRestoreInstanceState(state);消息= state.getString("tag");} 

When I set the tag of a View with setTag() dynamically it is not kept after an orientation change. The answers to a couple of related questions (these two answers in particular - 1 & 2) seem to suggest that tags are maintained on orientation changes (memory leaks if a View is stored in the tag means that its not released when it should be - i.e. orientation change). Is there any way to keep View tags after an orientation change (besides physically implementing your own method)?

I've done up a simple example where the tag is not saved on orientation change. The first Button is used to set the tag of the second Button. The second Button displays its current tag when clicked. On orientation change the tag is always null:

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        findViewById(R.id.set_tag).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                findViewById(R.id.display_tag).setTag("MY VIEW TAG");
                Toast.makeText(Main.this, "Tag set!", Toast.LENGTH_SHORT).show();
            }
        });
        findViewById(R.id.display_tag).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = (String) findViewById(R.id.display_tag).getTag();
                Toast.makeText(Main.this, "Tag is: "+((s == null) ? "null" : s), Toast.LENGTH_SHORT).show();
            }
        });
    }

}

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/set_tag"
        android:text="Set Tag"
        android:freezesText="true"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/display_tag"
        android:text="Display Tag"
        android:freezesText="true"/>
</LinearLayout>

Prior to orientation:

Post orientation:

解决方案

I don't think a tag is really what you want here. Consider using onSaveInstanceState and onRestoreInstanceState to preserve any data that needs to persist when the activity gets destroyed and recreated.

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString("tag", message);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(state);
    message = state.getString("tag");
}

这篇关于在方向更改时保持动态设置的View标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆