我有一个抽象基类,用于实例化一个片段的活动:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentContainer" android:layout_width="match_parent" android:layout_height="match_parent" >
</FrameLayout>
public abstract class SingleFragmentActivity extends Activity {
protected abstract Fragment createFragment();
private Fragment m_fragment;
public Fragment getSingleFragment() { return m_fragment; }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getFragmentManager();
m_fragment = fm.findFragmentById( R.id.fragmentContainer );
Log.d("SingleFragment", "fetching a fragment");
if ( m_fragment == null ) {
Log.d("SingleFragment", "SingleFragment requires creating a new one");
m_fragment = createFragment();
fm.beginTransaction().add( R.id.fragmentContainer, m_fragment ).commit();
} else {
Log.d("SingleFragment", "SingleFragment reusing one from fragment manager");
}
}
}
...加上一个简单的活动和片段:
public class LoginActivity extends SingleFragmentActivity {
private static final String TAG = "LOGIN ACTIVITY";
private LoginFragment m_loginFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG,"onCreate");
super.onCreate(savedInstanceState);
m_loginFragment = (LoginFragment) getSingleFragment();
Log.d(TAG,"...login fragment foo = "+m_loginFragment.getFoo());
m_loginFragment.setFoo( "Here's a new foo");
}
@Override
public void onPause() { super.onPause(); Log.d(TAG,"onPause"); }
@Override
public void onDestroy() { super.onDestroy(); Log.d(TAG,"onDestroy"); }
@Override
public void onResume() { super.onResume(); Log.d(TAG,"onResume"); }
@Override
protected Fragment createFragment() {
Log.d(TAG,"createFragment()");
return new LoginFragment();
}
}
public class LoginFragment extends Fragment {
private static final String TAG = "LOGIN FRAGMENT";
private String foo = "not set yet";
public String getFoo() { return foo; }
public void setFoo(String s) { foo = s; }
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate");
}
@Override
public void onPause() { super.onPause(); Log.d(TAG,"onPause"); }
@Override
public void onDestroy() { super.onDestroy(); Log.d(TAG,"onDestroy"); }
@Override
public void onResume() { super.onResume(); Log.d(TAG,"onResume"); }
}
如果我运行此代码,并在手机启动后旋转手机,我会看到以下日志:
08-07 15:02:51.310: LOGIN ACTIVITY: onCreate
08-07 15:02:51.340: SingleFragment: fetching a fragment
08-07 15:02:51.340: SingleFragment: SingleFragment requires creating a new one
08-07 15:02:51.340: LOGIN ACTIVITY: createFragment()
08-07 15:02:51.340: LOGIN ACTIVITY: ...login fragment foo = not set yet
08-07 15:02:51.340: LOGIN FRAGMENT: onCreate
08-07 15:02:51.340: LOGIN ACTIVITY: onResume
08-07 15:02:51.340: LOGIN FRAGMENT: onResume
08-07 15:02:59.799: LOGIN FRAGMENT: onPause
08-07 15:02:59.799: LOGIN ACTIVITY: onPause
08-07 15:02:59.809: LOGIN FRAGMENT: onDestroy
08-07 15:02:59.809: LOGIN ACTIVITY: onDestroy
08-07 15:02:59.829: LOGIN ACTIVITY: onCreate
08-07 15:02:59.829: LOGIN FRAGMENT: onCreate
08-07 15:02:59.869: SingleFragment: fetching a fragment
08-07 15:02:59.869: SingleFragment: SingleFragment reusing one from fragment manager
08-07 15:02:59.869: LOGIN ACTIVITY: ...login fragment foo = not set yet
08-07 15:02:59.869: LOGIN ACTIVITY: onResume
08-07 15:02:59.869: LOGIN FRAGMENT: onResume
一切都按预期工作:旋转手机会导致活动和碎片被破坏和重建;我看到片段管理器恢复某些东西,但我的问题是“什么?”成员变量foo不会被保留,那么正在恢复的是什么?
答案 0 :(得分:0)
我也遇到了同样的问题,有时候发布了一个问题 - After screen rotation, findFragmentById() returns a fragment, even if there's no such ID inside the layout
那时,http://developer.android.com/training/basics/fragments/creating.html的文件提到了这一点:
当配置更改导致托管这些活动时 要重新启动的片段,其新实例可能会使用不同的布局 不包含与先前布局相同的片段。在这 case仍然会实例化所有以前的片段 在新实例中运行。但是,任何不再是 与视图层次结构中的标记关联的将不具有它们 内容视图已创建,将从isInLayout()返回false。 (该 这里的代码还显示了如何确定片段是否放置在 容器不再在具有该容器的布局中运行 在这种情况下,请避免创建其视图层次结构。)
现在该部分在该文件中不存在。
如果此片段已添加到布局中,您可以查看isInLayout()
。