의 태그를 setTag()
으로 동적으로 설정하면 방향 변경 후에도 유지되지 않습니다. 두 개의 관련 질문에 대한 답변 (1 & 2)은 태그가 방향 변경 (태그에 View
이 저장되어있는 경우 메모리 누수가 발생했을 때 릴리스되지 않는다는 것을 의미합니다. 즉 방향 변경). 오리엔테이션을 변경 한 후에 View
태그를 유지할 수있는 방법이 있습니까 (실제로 자신의 메소드를 구현하는 것 외에)?방향 변경시 동적으로 설정된 뷰 태그 유지
태그가 방향 변경시 저장되지 않는 간단한 예제를 작성했습니다. 첫 번째 Button
은 두 번째 Button
의 태그를 설정하는 데 사용됩니다. 두 번째 Button
은 클릭 할 때 현재 태그를 표시합니다.
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>
전 오리엔테이션 :
포스트 방향 :