8

向きが変わると、フラグメントviewStateはでのみ復元されonStartます。、、、および後onAttachでも。なんで?これは手遅れです。onCreateViewonViewCreatedonActivityCreatedonCreate

TextView値に基づいてdbクエリ結果をListViewに入力する必要があります。現在、私はでこれをやろうとしていますonViewCreated。ただし、このステップではビューステートは復元されません。

早期に強制的に復元できますか?または、この問題を克服する方法は?何かアイデアをお願いします。

PS:私はactionbarsherlockと依存するAndroidサポートを使用しています-v4r7ライブラリ

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では、そのようなメソッドはありません。しかし、2つの解決策があります。

  1. 初期化中にビューステートに依存せずFragment、必要なすべての初期化ステートをステートとして保存しFragmentます(つまり、オーバーライドFragment#onSaveInstanceState())。onCreate()後で、、onCreateView()またはでフラグメントの状態を復元できますonViewCreated()
  2. 問題の指定に従って初期化を実行onStart()します。
于 2012-12-24T15:45:36.680 に答える
4

[編集1-----------]

//フラグメントバックスタックにデータが入力されているかどうかを確認します

//そうでない場合は、レイアウトを作成して入力します。

//フラグメントが再作成されないようにします

  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 に答える