Android的碎片setRetainInstance(真)不工作(Android的支持库) [英] Android fragments setRetainInstance(true) not works (Android support library)
本文介绍了Android的碎片setRetainInstance(真)不工作(Android的支持库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有储蓄片段状态的问题。我尝试使用setRetainInstance,但不能使它工作(((我改变一国2使用按钮1,但改变屏幕方向后,我看到1时pressing的按钮2。我的错误?
公共类TestFragment扩展片段{
@覆盖
公共无效的onCreate(包savedInstanceState){
setRetainInstance(真正的);
super.onCreate(savedInstanceState);
}
私人字符串状态=1;
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看查看= inflater.inflate(R.layout.fragment_layout,集装箱,假);
//按钮改变状态
((按钮)view.findViewById(R.id.button1))。setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看为arg0){
状态=2;
}
});
//按钮显示状态
((按钮)view.findViewById(R.id.button2))。setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看为arg0){
Toast.makeText(getActivity(),州,Toast.LENGTH_SHORT).show();
}
});
返回查看;
}
}
修改
活动:
公共类MainActivity扩展FragmentActivity {
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
}
}
活动的布局:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
<片段
机器人:名称=ru.ee.TestFragment
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
/>
< / RelativeLayout的>
片段布局:
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=更改为2/>
<按钮
机器人:ID =@ + ID /按钮2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=显示/>
< / LinearLayout中>
清单XML:
<舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=ru.ee.testfrag
安卓版code =1
机器人:VERSIONNAME =1.0>
<用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =15/>
<应用
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:名称=ru.ee.MainActivity
机器人:标签=@字符串/ title_activity_main>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途>
< /舱单>
解决方案
您片段只会得到重复使用,如果你给片段的标识布局,改变
<片段
机器人:名称=ru.ee.TestFragment
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
/>
是
<片段
机器人:ID =@ + ID / a_fragment
机器人:名称=ru.ee.TestFragment
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
/>
I have a problem with saving fragment state. I try to use setRetainInstance, but cant make it work((( I change a state to 2 using button1, but after changing screen orientation i see 1 when pressing on button2. Where is my mistake?
public class TestFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
super.onCreate(savedInstanceState);
}
private String state = "1";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, container, false);
//button for changing state
((Button)view.findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
state = "2";
}
});
//button for showing state
((Button)view.findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(getActivity(), state, Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
EDIT
Activity:
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:name="ru.ee.TestFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Fragment layout:
<?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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change to 2" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show" />
</LinearLayout>
Manifest XML:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.ee.testfrag"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="ru.ee.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
解决方案
Your fragment will only get re-used if you give the fragment an Id in the layout, change
<fragment
android:name="ru.ee.TestFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
to be
<fragment
android:id="@+id/a_fragment"
android:name="ru.ee.TestFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
这篇关于Android的碎片setRetainInstance(真)不工作(Android的支持库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文