【发布时间】:2012-01-20 03:19:00
【问题描述】:
我有一个视图,其可见性在布局 xml 中设置为 GONE,并且可以在 Fragment 中以编程方式设置为 VISIBLE。当使用 Back 按钮返回此 Fragment 时,View 的可见性始终设置为 GONE,即使在离开之前已设置为 VISIBLE。
如何保持片段的状态(返回片段时仅调用 onResume())?
谢谢
XML
...
<LinearLayout
android:id="@+id/sub_bio_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" />
....
活动
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
LinearLayout sub_bio_container = (LinearLayout) findViewById(R.id.sub_bio_container);
if(<some condition>){
sub_bio_container.setVisibility(VIEW.VISIBLE);
}
....
}
【问题讨论】:
-
如果您可以发布您的 onStart、onResume、onPause 和 onStop 方法,那将是最有帮助的。
-
@David Burström:我在哪里保存视图的可见性状态?
-
@user370305:我在帖子中添加了代码。
-
此代码是否在活动的 onCreate() 方法中?
-
在片段的
onCreateView()方法中。
标签: android android-activity visibility