避免在屏幕旋转后重新创建片段的真实方法(以官方片段开发者指南为例)



我正在寻找一种真正的方法来避免在屏幕旋转后重新创建片段

if (container == null) { return null; }而不是真正避免了要重新创建的片段。(如下图所示)


官方碎片开发者指南在哪里

我们关心的官方指南在http://developer.android.com/guide/components/fragments.html.部分示例代码位于指南的底部。据我所知,完整的示例代码在Android3.0(API 11)下的"SDK示例"中提供。此外,我对示例代码做了最小的修改,以便在API 10中运行,并添加了一些调试消息,这些消息包含在本问题的底部。

R.id.a_item在哪里

您可以在开发人员指南示例中找到以下代码存根:

if (index == 0) {
ft.replace(R.id.details, details);
} else {
ft.replace(R.id.a_item, details);
}

我在网上搜索了一下,发现其他人也在关注R.id.a_item在哪里。在检查了API 11中的样本后,我确信这只是一个无意义的打字错误。样本中根本没有这样的线条。


避免在屏幕旋转后重新创建片段的真实方法

网上已有许多讨论。但似乎还没有一个"真正的"解决方案

我在下面的代码中添加了许多调试消息,以跟踪DetailsFragment类的生命周期。尝试(1)在纵向模式下启动程序,然后(2)将设备转为横向模式,然后(3)将其转回纵向模式,(4)再次转为横向,(5)再次转回纵向,最后(6)退出。我们将收到以下调试消息:

(1) 在纵向模式下启动

TitlesFFragment.onCreate()Bundle=null

仅创建TitlesFragment。CCD_ 6还未示出。

(2) 进入横向模式

TitlesFFragment.onCreate()Bundle=Bundle[{shownChoice=-1,android:view_state=android.util.SparseArray@4051d3a8,curChoice=0}]
DetailsFragment.onAttach()活动=com.example.android.apis.app.FragmentLayout@4051d640
DetailsFragment.onCreate()Bundle=null
DetailsFragments.onCreateView()活动=android.widget.FrameLayout@4050df68
DetailsFragment.onActivityCreated()Bundle=null
DetailsFragments.onStart()
Details Frament.onResume()

首先,将重新创建TitlesFragment(使用savedInstanceState Bundle)。然后动态创建DetailsFragment(由TitlesFragment.onActivityCreated()调用showDetails(),使用FragmentTransaction)。

(3) 返回纵向模式

DetailsFragment.onPause=com.example.android.apis.app.FragmentLayout@40527f70
DetailsFragment.onCreate()Bundle=null
TitlesFragment.onCreate=android.util.SparseArray@405144b0,curChoice=0}]
DetailsFragment.onCreateView

这是我们首先关注的real重新创建避免方法。

这是因为DetailsFragment先前在横向模式下附接到layout-land/fragment_layout.xml<FrameLayout>ViewGroup。并且它具有一个ID(R.id.details)。当屏幕旋转时,作为DetailsFragment实例的ViewGroup会保存到FragmentLayout的onSaveInstanceState()中的Activity FragmentLayout's Bundle中。进入人像模式后,将重新创建DetailsFragment。但在人像模式下不需要

在示例中(正如许多其他人所建议的那样),DetailsFragment类在onCreateView()中使用if (container == null) { return null; },以避免DetailsFragment以纵向模式显示。然而,如上面的调试消息所示,DetailsFragment在后台仍然是活动的,作为孤儿,具有所有的生命周期方法调用。

(4) 再次进入横向模式

DetailsFragment.onPause=com.example.android.apis.app.FragmentLayout@4052c7d8
DetailsFragment.onCreate()Bundle=null
TitlesFragment.onCreate=android.util.SparseArray@40521b80,curChoice=0}]
DetailsFragment.onCreateView()活动=android.widget.FrameLayout@40525270
DetailsFragment.onActivityCreated()Bundle=null
DetailsFragments.onStart()
Details Frament.onResume()

请注意,在前5行中,DetailsFragment完成了其生命周期状态,然后销毁并分离。

这进一步证明了if (container == null) { return null; }方法是而不是一种real方法来摆脱DetailsFragment实例。(我以为垃圾回收器会销毁这个悬空的孩子,但它没有。这是因为Android确实允许一个悬空的片段。参考:添加一个没有UI的片段。)

据我所知,从第6行开始,它应该是由TitlesFragment创建的一个新的DetailsFragment实例,就像在(2)中所做的那样。但我无法解释为什么DetailsFragmentonAttach()onCreate()方法在TitlesFragmentonCreate()之前被调用。

但是DetailsFragmentonCreate()中的nullBundle将证明它是一个新的实例。

据我所知,上一个悬挂的DetailsFragment实例这次没有重新创建,因为它没有ID。因此,它没有使用视图层次结构自动保存到savedInstanceState Bundle中。

(5) 再次返回纵向模式

DetailsFragment.onPause=com.example.android.apis.app.FragmentLayout@4052d7d8
DetailsFragment.onCreate()Bundle=null
TitlesFragment.onCreate=android.util.SparseArray@40534e30,curChoice=0}]
DetailsFragment.onCreateView

请注意,所有生命周期回调(3)中第一次返回肖像的相同,不同的Activity ID (40527f70 vs 4052d7d8)view_state Bundle (405144b0 vs 40534e30)除外。这是合理的。FragmentLayout活动和实例状态捆绑都将重新创建。

(6)退出(按返回按钮)

I/System.out(29845):DetailsFragment.onPause()I/System.out(29845):DetailsFragment.onStop()I/System.out(29845):DetailsFragment.onDestroyView()I/System.out(29845):DetailsFragment.onDestroy()I/System.out(29845):DetailsFragment.onDetach()

如果我们能删除FragmentLayoutonDestroy()中的DetailsFragment,那将是完美的。但是FragmentTransactionremove()方法需要在onSaveInstanceState()之前调用。但是,在onSaveInstanceState()中无法确定是否为屏幕旋转。

无论如何也不可能删除FragmentLayoutonSaveInstanceState()中的DetailsFragment。首先,如果DetailsFragment只是被对话框部分遮挡,它将在背景中消失。此外,在被对话框遮挡或切换活动的情况下,onCreate(Bundle)onRestoreInstanceState(Bundle)都不会被再次调用。因此,我们没有在哪里恢复片段(并从Bundle中检索数据)。


源代码&文件

FragmentLayout.java

package com.example.android.apis.app;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
public class FragmentLayout extends FragmentActivity {
private final static class Shakespeare {
public static final String[] TITLES = { "Love", "Hate", "One", "Day" };
public static final String[] DIALOGUE = {
"Love Love Love Love Love",
"Hate Hate Hate Hate Hate",
"One One One One One",
"Day Day Day Day Day" };
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
}
public static class DetailsActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
// If the screen is now in landscape mode, we can show the
// dialog in-line with the list so we don't need this activity.
finish();
return;
}
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
}
}
public static class TitlesFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
int mShownCheckPosition = -1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println(getClass().getSimpleName() + ".onCreate() Bundle=" + 
(savedInstanceState == null ? null : savedInstanceState));
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Populate list with our static array of titles.
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, Shakespeare.TITLES));
// API 11:android.R.layout.simple_list_item_activated_1
// Check to see if we have a frame in which to embed the details
// fragment directly in the containing UI.
View detailsFrame = getActivity().findViewById(R.id.details);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null) {
// Restore last state for checked position.
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
mShownCheckPosition = savedInstanceState.getInt("shownChoice", -1);
}
if (mDualPane) {
// In dual-pane mode, the list view highlights the selected item.
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// Make sure our UI is in the correct state.
showDetails(mCurCheckPosition);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
outState.putInt("shownChoice", mShownCheckPosition);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
/**
* Helper function to show the details of a selected item, either by
* displaying a fragment in-place in the current UI, or starting a
* whole new activity in which it is displayed.
*/
void showDetails(int index) {
mCurCheckPosition = index;
if (mDualPane) {
// We can display everything in-place with fragments, so update
// the list to highlight the selected item and show the data.
getListView().setItemChecked(index, true);
if (mShownCheckPosition != mCurCheckPosition) {
// If we are not currently showing a fragment for the new
// position, we need to create and install a new one.
DetailsFragment df = DetailsFragment.newInstance(index);
// Execute a transaction, replacing any existing fragment
// with this one inside the frame.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, df);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
mShownCheckPosition = index;
}
} else {
// Otherwise we need to launch a new activity to display
// the dialog fragment with selected text.
Intent intent = new Intent();
intent.setClass(getActivity(), DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}
public static class DetailsFragment extends Fragment {
/**
* Create a new instance of DetailsFragment, initialized to
* show the text at 'index'.
*/
public static DetailsFragment newInstance(int index) {
DetailsFragment f = new DetailsFragment();
// Supply index input as an argument.
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println(getClass().getSimpleName() + ".onCreate() Bundle=" + 
(savedInstanceState == null ? null : savedInstanceState));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
System.out.println(getClass().getSimpleName() + ".onAttach() Activity=" + 
(activity == null ? null : activity));
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
System.out.println(getClass().getSimpleName() + ".onActivityCreated() Bundle=" + 
(savedInstanceState == null ? null : savedInstanceState));
}
@Override
public void onStart() { super.onStart(); System.out.println(getClass().getSimpleName() + ".onStart()"); }
@Override
public void onResume() { super.onResume(); System.out.println(getClass().getSimpleName() + ".onResume()"); }
@Override
public void onPause() { super.onPause(); System.out.println(getClass().getSimpleName() + ".onPause()"); }
@Override
public void onStop() { super.onStop(); System.out.println(getClass().getSimpleName() + ".onStop()"); }
@Override
public void onDestroyView() { super.onDestroyView(); System.out.println(getClass().getSimpleName() + ".onDestroyView()"); }
@Override
public void onDestroy() { super.onDestroy(); System.out.println(getClass().getSimpleName() + ".onDestroy()"); }
@Override
public void onDetach() { super.onDetach(); System.out.println(getClass().getSimpleName() + ".onDetach()"); }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println(getClass().getSimpleName() + ".onCreateView() Activity=" + 
(container == null ? null : container));
if (container == null) {
// We have different layouts, and in one of them this
// fragment's containing frame doesn't exist.  The fragment
// may still be created from its saved state, but there is
// no reason to try to create its view hierarchy because it
// won't be displayed.  Note this is not needed -- we could
// just run the code below, where we would create and return
// the view hierarchy; it would just never be used.
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(Shakespeare.DIALOGUE[getArguments().getInt("index", 0)]);
return scroller;
}
}
}

layout/fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
android:id="@+id/titles"
android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>

布局土地/fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:baselineAligned="false"
android:layout_width="match_parent" android:layout_height="match_parent">
<fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
android:id="@+id/titles" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" />
<FrameLayout android:id="@+id/details" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" />
<!-- API 11:android:background="?android:attr/detailsElementBackground" -->
</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.apis.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android.apis.app.FragmentLayout"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.android.apis.app.FragmentLayout$DetailsActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>

我使用以下方法取得了一些成功。

这将根据片段的状态执行正确的操作。

public void activate(FragmentTransaction ft, Fragment f, String tag, int resId) {
boolean changed =   resId != f.getId();
if (changed && (f.isAdded() || f.isDetached())) {
ft.remove(f);
ft.add(resId, f, tag);
return;
}
// Currently in a detached mode
if (f.isDetached()) {
ft.attach(f);
return;
}
// Not in fragment manager add
if (!f.isAdded() && ! f.isDetached()) {
ft.add(resId, f, tag);
return;
}
}

这将处理特定的记忆片段。

private enum FragmentOp {
ADD
,DETACH
,REMOVE
;
}
private void ensureFragmentState(FragmentOp op) {
if (null == iChild) {
iChild = (FragmentChild) iFM.findFragmentById(R.id.fragment_phonenumber_details);
}
FragmentTransaction ft = iFM.beginTransaction();
switch(op) {
case ADD:
if (null == iChild) {
iChild = new FragmentChild();
}
activate(ft, iChild, null, R.id.fragment_phonenumber_details);
break;
case DETACH:
if (null != iChild) {
iChild.deactivate(ft);
}
break;
case REMOVE:
if (null != iChild) {
iChild.remove(ft);
}
break;
}
// Only if something shows up did we do anything!
if (null != iChild) {
ft.commit();
}
}

然后在生命周期中的方法:

@Override public void onResume() {
super.onResume();
if (iDualPane) {
ensureFragmentState(FragmentOp.ADD);
}
}
@Override public void onPause() {
super.onPause();
recordState();   // Grab what I need from the child fragment
ensureFragmentState(FragmentOp.DETACH);
}

非常感谢@RogerGarzonNieto发现了在方向更改时禁用自动活动重新创建的方法。它非常有用。我相信在未来的某些情况下我将不得不使用它。

为了避免在屏幕旋转时重新创建碎片,我发现了一种更简单的方法,我们可以仍然允许活动像往常一样重新创建。

onSaveInstanceState():中

@Override
protected void onSaveInstanceState(Bundle outState) {
if (isPortrait2Landscape()) {
remove_fragments();
}
super.onSaveInstanceState(outState);
}
private boolean isPortrait2Landscape() {
return isDevicePortrait() && (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
}

并且CCD_ 55将类似于:

private boolean isDevicePortrait() {
return (findViewById(R.id.A_View_Only_In_Portrait) != null);
}

*请注意,我们不能使用getResources().getConfiguration().orientation来确定设备当前是否为肖像。这是因为Resources对象更改了RIGHT AFTER屏幕旋转-EVEN BEFOREonSaveInstanceState()被调用!!

如果您不想使用findViewById()来测试方向(出于任何原因,而且它毕竟不太整洁),请保留一个全局变量DetailsFragment0,并在onCreate()中用current_orientation = getResources().getConfiguration().orientation;初始化它。这看起来更整洁。但我们应该注意不要在"活动"生命周期中的任何地方更改它。

*请确保我们在super.onSaveInstanceState()之前remove_fragments()

(因为在我的情况下,我会从布局和活动中删除碎片。如果是在super.onSaveInstanceState()之后,布局将已经保存到捆绑包中。然后,在活动重新创建后,碎片也将被重新创建。###)

###我已经证明了这一现象。但是的原因是什么?在活动重新创建时,如何确定碎片恢复只是我的猜测。如果你对此有任何想法,请回答我的另一个问题。谢谢

最新更新