【问题标题】:Android fragments setRetainInstance(true) not works (Android support library)Android 片段 setRetainInstance(true) 不起作用(Android 支持库)
【发布时间】:2012-10-15 18:28:27
【问题描述】:

我在保存片段状态时遇到问题。我尝试使用 setRetainInstance,但无法使其工作(((我使用 button1 将状态更改为 2,但在更改屏幕方向后,按下 button2 时我看到 1。我的错误在哪里?

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;
    }

}

编辑

活动:

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

活动布局:

<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>

片段布局:

<?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>

清单 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>

【问题讨论】:

    标签: android android-fragments android-support-library


    【解决方案1】:

    只有在布局中为片段指定 ID 时,您的片段才会被重新使用,更改

    <fragment
        android:name="ru.ee.TestFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    

    成为

    <fragment
        android:id="@+id/a_fragment"
        android:name="ru.ee.TestFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    

    【讨论】:

    • 非常感谢!无休止地寻找这种东西对我的健康非常不利。我很高兴它结束了!
    • 如果 saveInstanceState() 真的在布局中没有片段 ID 的情况下不会生效,否则会静默失败,那真的应该在 saveInstanceState() 的文档中说明!
    【解决方案2】:

    您发布的代码没有任何问题。如果您正确实现了片段,它应该给您状态 2。也许您可以发布您的活动和 xml 文件。一定是其他地方有错误。

    【讨论】:

    • 我添加了activity和xml代码,有时间请看一下。我已经看过这个代码一百遍了,但不明白我的错误在哪里(((
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多