8

当方向改变时,片段viewState仅在onStart. 之后onAttach,,甚至之后。onCreateView_ 为什么?这为时已晚。onViewCreatedonActivityCreatedonCreate

我需要根据一些 TextView 值将数据库查询结果填充到 ListView。目前我尝试在onViewCreated. 但是在这一步不会恢复视图状态。

我可以强制提前恢复吗?或者如何克服这个问题?请有任何想法。

PS:我使用actionbarsherlock和依赖的android support-v4 r7库

PS2:如果我要加载数据,onStart那么它会在片段恢复后执行额外的查询onStop(我可以通过添加一些布尔值来解决这个问题isLoaded——但这不是最好的解决方案)。

4

3 回答 3

13

在 Android API >= 17 (Android 4.2 Jelly Beans) 中有一个方法: public void onViewStateRestored (Bundle savedInstanceState)

如文档中所述,它在之前onStart()和之后被调用。onActivityCreated()

在 Android API < 17 中没有这样的方法。但是有两种解决方案:

  1. 初始化时不要依赖视图状态Fragment,并将所有必需的初始化状态保存为Fragment状态(即覆盖Fragment#onSaveInstanceState())。稍后您可以在或onCreate()中恢复片段状态。onCreateView()onViewCreated()
  2. onStart()按照相关规定执行初始化。
于 2012-12-24T15:45:36.680 回答
4

[编辑 1 - - - - - - -]

// 检查 Fragment 回栈是否已被填充

// 如果不是,则创建并填充布局。

// 所以你的片段不会重新创建

  YourFragment yourFragment = (YourFragment )fm.findFragmentById(R.id.fragment_container);

  if (yourFragment == null) {
       FragmentTransaction ft = fm.beginTransaction(); 
       ft.replace(R.id.fragment_container, new YourFragment ());       
       ft.commit();
  }

[编辑 1 - - - - - - -]

在此处输入图像描述

/**
 * Listing 4-4: Fragment skeleton code
 * Listing 4-5: Fragment lifecycle event handlers
 */
package com.paad.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MySkeletonFragment extends Fragment {

  // Called when the Fragment is attached to its parent Activity.
  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Get a reference to the parent Activity.
  }

  // Called to do the initial creation of the Fragment.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Initialize the Fragment.
  }

  // Called once the Fragment has been created in order for it to
  // create its user interface.
  @Override
  public View onCreateView(LayoutInflater inflater, 
                           ViewGroup container,
                           Bundle savedInstanceState) {
    // Create, or inflate the Fragment's UI, and return it. 
    // If this Fragment has no UI then return null.
    return inflater.inflate(R.layout.my_fragment, container, false);
  }



  // Called once the parent Activity and the Fragment's UI have 
  // been created.
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Complete the Fragment initialization Ğ particularly anything
    // that requires the parent Activity to be initialized or the 
    // Fragment's view to be fully inflated.
  }

  // Called at the start of the visible lifetime.
  @Override
  public void onStart(){
    super.onStart();
    // Apply any required UI change now that the Fragment is visible.
  }

  // Called at the start of the active lifetime.
  @Override
  public void onResume(){
    super.onResume();
    // Resume any paused UI updates, threads, or processes required
    // by the Fragment but suspended when it became inactive.
  }

  // Called at the end of the active lifetime.
  @Override
  public void onPause(){
    // Suspend UI updates, threads, or CPU intensive processes
    // that don't need to be updated when the Activity isn't
    // the active foreground activity.
    // Persist all edits or state changes
    // as after this call the process is likely to be killed.
    super.onPause();
  }

  // Called to save UI state changes at the
  // end of the active lifecycle.
  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save UI state changes to the savedInstanceState.
    // This bundle will be passed to onCreate, onCreateView, and
    // onCreateView if the parent Activity is killed and restarted.
    super.onSaveInstanceState(savedInstanceState);
  }

  // Called at the end of the visible lifetime.
  @Override
  public void onStop(){
    // Suspend remaining UI updates, threads, or processing
    // that aren't required when the Fragment isn't visible.
    super.onStop();
  }

  // Called when the Fragment's View has been detached.
  @Override
  public void onDestroyView() {
    // Clean up resources related to the View.
    super.onDestroyView();
  }

  // Called at the end of the full lifetime.
  @Override
  public void onDestroy(){
    // Clean up any resources including ending threads,
    // closing database connections etc.
    super.onDestroy();
  }

  // Called when the Fragment has been detached from its parent Activity.
  @Override
  public void onDetach() {
    super.onDetach();
  }
}

来源:Professional Android Development 4 - Reto Meier

于 2012-12-24T12:56:30.780 回答
0

您始终可以自己处理方向更改onConfigurationChanged()。在这里查看一个很好的例子http://alexfu.tumblr.com/post/13926762386/android-dev-handling-fragment-recreation-manually

于 2012-12-24T14:30:35.520 回答