【发布时间】:2016-11-12 00:18:48
【问题描述】:
当一个 Activity 生成一个 Fragment 并在稍后重新创建时(例如,通过旋转屏幕),与该 Fragment 关联的视图将被复制,当该 Fragment 稍后被销毁时,只有一个被销毁。
当且仅当 Activity 直接在其 onSaveInstanceState 覆盖或不覆盖回调时调用 super.onSaveInstanceState 时才会发生这种情况。
重现的最少代码: MainActivity.java:
package com.example.trevor.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
/**
* Created by trevor on 11/11/16.
*/
public class MainActivity extends Activity {
MainFragment fragment = new MainFragment();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox checkbox = (CheckBox)findViewById(R.id.checkBox);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b)
{
getFragmentManager().beginTransaction().add(R.id.container,fragment).commit();
}
else
{
getFragmentManager().beginTransaction().remove(fragment).commit();
}
}
});
}
}
MainFragment.java:
package com.example.trevor.test;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by trevor on 11/11/16.
*/
public class MainFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_main,container,false);
}
}
activity_main.xml:
<?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">
<CheckBox
android:text="CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/checkBox" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
</FrameLayout>
</LinearLayout>
fragment_main.xml:
<?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">
<TextView
android:text="Open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView" />
</LinearLayout>
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.trevor.test">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
预期行为:选中该框会导致“打开”一词出现在下方。取消选中会导致单词消失。
实际行为:选中该框会使“打开”一词出现在下方。如果随后旋转屏幕,“open”一词会变暗,取消选中该框会使该词变为正常阴影。
【问题讨论】: