2012-12-24 15 views
8

Quando l'orientamento cambia, frammento viewState ripristinato solo in onStart. Dopo onAttach, onCreateView, onViewCreated e onActivityCreated e anche dopo lo onCreate. Perché? È troppo tardiFrammento ViewState ripristinato in onStart?

Ho bisogno di popolare i risultati delle query db a ListView basate su un certo valore TextView. Attualmente provo a farlo in onViewCreated. Ma lo stato di visualizzazione non viene ripristinato in questo passaggio.

posso forzare il ripristino presto? O come superare questo problema? Qualche idea, per favore.

PS: io uso ActionBarSherlock e biblioteca r7 android supporto-v4 dipendente

PS2: se io caricare i dati in onStart allora farà query aggiuntive quando frammento viene ripresa dopo onStop (posso risolvere questo aggiungendo qualche booleano isLoaded - ma questa non è la soluzione migliore).

risposta

12

In API Android> = 17 (fagioli di gelatina Android 4.2) c'è un metodo: public void onViewStateRestored (Bundle savedInstanceState)

che è chiamato prima e dopo onStart()onActivityCreated() come menzionato in docs.

In Android API < 17 non esiste tale metodo. Ma ci sono due soluzioni:

  1. non si basano su stato di visualizzazione durante l'inizializzazione Fragment e salvare tutto lo stato di inizializzazione richiesto come Fragment Stato (vale a dire ignorare Fragment#onSaveInstanceState()). Successivamente è possibile ripristinare lo stato dei frammenti in onCreate(), onCreateView() o onViewCreated().
  2. eseguire l'inizializzazione in onStart() come specificato in questione.
4

[EDIT 1 - - - - - - -]

// Verificare se lo stack frammento posteriore è stata popolata

// In caso contrario, creare e popolare il layout.

// in modo che il frammento di solito ricreato

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(); 
    } 

[EDIT 1 - - - - - - -]

enter image description here

/** 
* 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(); 
    } 
} 

fonte: professionista Android Development 4 - Reto Meier

+0

in 'vista onAttach' non esiste. Come specificato nel vostro commento nella vista 'onActivityCreated', ma lo stato di visualizzazione del frammento salvato non è ancora stato ripristinato. – acc15

+0

Sì vista è creata suCreateView. – Talha

+0

LayoutView restituito da onCreateView non è inizializzato e contiene i valori iniziali. Ho bisogno di valori ripristinati nel momento in cui l'orientamento cambia. – acc15

Problemi correlati