if (container == null) { return null; }
不真的避免重新创建片段。 (如下图所示)
我们关注的官方指南是http://developer.android.com/guide/components/fragments.html。 partial example code位于指南的底部。据我所知,完整的示例代码可以在" Samples for SDK"在Android 3.0(API 11)下。此外,我对示例代码进行了少量修改,使其在API 10中运行,并添加了一些调试消息,这些消息包含在本问题的底部。
R.id.a_item
在哪里?您可以在Developer Guide Example中找到以下代码存根:
if (index == 0) {
ft.replace(R.id.details, details);
} else {
ft.replace(R.id.a_item, details);
}
我在互联网上进行了一些搜索,发现some others也涉及到R.id.a_item
的位置。在API 11中检查样本后,我确信它只是一个毫无意义的错误。样本中根本没有这样的行。
网上有很多现有的讨论。但似乎没有真正的"解决方案。
我在下面的代码中添加了大量调试消息,以跟踪DetailsFragment
类的生命周期。尝试(1)以纵向模式启动程序,然后(2)将设备转为横向模式,然后(3)将其转回肖像,(4)再次横向渲染,(5)再次回到肖像,最后(6)退出。我们将有以下调试消息:
(1)以肖像模式启动
TitlesFragment.onCreate()Bundle = null
仅创建了TitlesFragment
。 DetailsFragment
尚未显示。
(2)进入风景模式
TitlesFragment.onCreate()Bundle = Bundle [{shownChoice = -1,android:view_state=android.util.SparseArray@4051d3a8,curChoice = 0}]
DetailsFragment.onAttach()Activity = com.example.android.apis.app.FragmentLayout@4051d640
DetailsFragment.onCreate()Bundle = null
DetailsFragment.onCreateView()Activity=android.widget.FrameLayout@4050df68
DetailsFragment.onActivityCreated()Bundle = null
DetailsFragment.onStart()
DetailsFragment.onResume()
首先,重新创建TitlesFragment
(使用savedInstanceState Bundle)。然后DetailsFragment
动态创建TitlesFragment.onActivityCreated()
,使用showDetails()
调用FragmentTransaction
。
(3)返回肖像模式
DetailsFragment.onPause()
DetailsFragment.onStop()
DetailsFragment.onDestroyView()
DetailsFragment.onDestroy()
DetailsFragment.onDetach()
DetailsFragment.onAttach()Activity = com.example.android.apis.app.FragmentLayout@40527f70
DetailsFragment.onCreate()Bundle = null
TitlesFragment.onCreate()Bundle = Bundle [{shownChoice = 0,android:view_state=android.util.SparseArray@405144b0,curChoice = 0}]
DetailsFragment.onCreateView()Activity = null
DetailsFragment.onActivityCreated()Bundle = null
DetailsFragment.onStart()
DetailsFragment.onResume()
以下是关于 真实 重新创建避免方法的第一个问题。
这是因为DetailsFragment
之前已在横向模式下附加到layout-land/fragment_layout.xml
<FrameLayout>
ViewGroup
。它有一个ID(R.id.details
)。当屏幕旋转时,作为ViewGroup
的实例的DetailsFragment
被保存到FragmentLayout的onSaveInstanceState()中的Activity FragmentLayout&lt; Bundle中。进入纵向模式后,将重新创建DetailsFragment
。但在纵向模式下需要不。
在示例中(以及其他许多建议),DetailsFragment
班级使用if (container == null) { return null; }
中的onCreateView()
来避免DetailsFragment
以纵向模式显示。但是,如上面的调试消息所示,DetailsFragment
在后台仍然存在,作为孤儿,具有所有生命周期方法调用。
(4)再次风景模式
DetailsFragment.onPause()
DetailsFragment.onStop()
DetailsFragment.onDestroyView()
DetailsFragment.onDestroy()
DetailsFragment.onDetach()
DetailsFragment.onAttach()Activity = com.example.android.apis.app.FragmentLayout@4052c7d8
DetailsFragment.onCreate()Bundle = null
TitlesFragment.onCreate()Bundle = Bundle [{shownChoice = 0,android:view_state=android.util.SparseArray@40521b80,curChoice = 0}]
DetailsFragment.onCreateView()Activity = android.widget.FrameLayout@40525270
DetailsFragment.onActivityCreated()Bundle = null
DetailsFragment.onStart()
DetailsFragment.onResume()
注意在前5行中,DetailsFragment
完成其生命周期状态,然后销毁和分离。
这进一步证明了if (container == null) { return null; }
方法 真正的 方法来摆脱DetailsFragment
实例。 (我以为垃圾收集者会摧毁这个晃来晃去的孩子,但它没有。这是因为Android确实允许悬挂片段。参考:Adding a fragment without a UI。)
据我所知,从第6行开始,它应该是由DetailsFragment
创建的新TitlesFragment
实例,就像它在(2)中所做的那样。但我无法解释为什么在DetailsFragment
onAttach()
之前调用onCreate()
TitlesFragment
和onCreate()
方法。
但null
&#39; DetailsFragment
中的onCreate()
捆绑包会证明它是一个新实例。
根据我的理解,之前的悬空DetailsFragment
实例不会重新创建,因为它没有ID。因此它没有使用视图层次结构自动保存到savedInstanceState Bundle中。
(5)再次返回人像模式
DetailsFragment.onPause()
DetailsFragment.onStop()
DetailsFragment.onDestroyView()
DetailsFragment.onDestroy()
DetailsFragment.onDetach()
DetailsFragment.onAttach()Activity = com.example.android.apis.app.FragmentLayout@4052d7d8
DetailsFragment.onCreate()Bundle = null
TitlesFragment.onCreate()Bundle = Bundle [{shownChoice = 0,android:view_state=android.util.SparseArray@40534e30,curChoice = 0}]
DetailsFragment.onCreateView()Activity = null
DetailsFragment.onActivityCreated()Bundle = null
DetailsFragment.onStart()
DetailsFragment.onResume()
请注意,所有生命周期回调在(3)中第一次回到肖像时与完全相同,除了Activity ID (40527f70 vs 4052d7d8)
和{{ 1}}。这是合理的。将重新创建FragmentLayout活动和实例状态包。
(6)退出(按BACK按钮)
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()
如果我们可以删除view_state Bundle (405144b0 vs 40534e30)
DetailsFragment
中的FragmentLayout
,那将是完美的。但需要在onDestroy()
之前调用FragmentTransaction
remove()
方法。但是,onSaveInstanceState()
无法确定屏幕是否旋转。
无论如何,也无法移除onSaveInstanceState()
DetailsFragment
中的FragmentLayout
。首先,如果onSaveInstanceState()
被对话框部分遮挡,它将在后台消失。此外,如果被对话框遮挡或切换活动,则不会再次调用DetailsFragment
和onCreate(Bundle)
。因此,我们无法恢复片段(并从Bundle中检索数据)。
FragmentLayout.java
onRestoreInstanceState(Bundle)
布局/ fragment_layout.xml
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;
}
}
}
布局脊/ 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>
的AndroidManifest.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>
答案 0 :(得分:0)
我使用以下方法取得了一些成功。
根据片段的状态,这是正确的。
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;
}
}
它处理特定的memoized片段。
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);
}
答案 1 :(得分:-2)
非常感谢@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);
}
和isDevicePortrait()
就像:
private boolean isDevicePortrait() {
return (findViewById(R.id.A_View_Only_In_Portrait) != null);
}
*请注意,我们 无法 使用getResources().getConfiguration().orientation
来确定设备当前是字面纵向。这是因为Resources
对象在屏幕旋转后更改 RIGHT AFTER - 以前 {{ 1}}被称为!!
如果您不想使用onSaveInstanceState()
来测试方向(由于任何原因,并且它不是那么整洁),请保留一个全局变量findViewById()
并将其初始化为private int current_orientation;
in current_orientation = getResources().getConfiguration().orientation;
。这似乎更整洁。但我们应该注意 不要 在活动生命周期中的任何地方进行更改。
*确保我们onCreate()
之前 remove_fragments()
。
(因为在我的情况下,我从布局和活动中删除碎片。如果它在super.onSaveInstanceState()
之后,布局将已经保存到Bundle中。然后碎片将<在活动重新创建后重新创建strong> 。###)
###我已经证明了这一现象。但What to determine a Fragment restore upon Activity re-create?的原因仅仅是我的猜测。如果您对此有任何想法,请回答my another question。谢谢!