2012-12-24 11 views
8

जब अभिविन्यास परिवर्तन, खंड viewState केवल onStart में बहाल किया गया। onAttach, onCreateView, onViewCreated और onActivityCreated और onCreate के बाद भी। क्यों? यह बहुत देर हो चुकी है।फ्रैगमेंट व्यूस्टेट ऑनस्टार्ट में बहाल?

मुझे कुछ टेक्स्ट व्यू मान के आधार पर सूची दृश्य में डीबी क्वेरी परिणाम पॉप्युलेट करने की आवश्यकता है। वर्तमान में मैं इसे onViewCreated में करने का प्रयास करता हूं। लेकिन इस कदम पर राज्य को बहाल नहीं किया गया है।

क्या मैं जल्दी बहाल कर सकता हूं? या इस समस्या को दूर करने के लिए कैसे? कोई विचार, कृपया।

पुनश्च: मैं ActionBarSherlock और निर्भर एंड्रॉयड समर्थन-v4 R7 लाइब्रेरी का उपयोग

पीएस 2: अगर मैं onStart में डेटा लोड होगा तो यह अतिरिक्त प्रश्नों क्या करेंगे जब टुकड़ा के बाद onStop (मैं कुछ जोड़कर इस हल कर सकते हैं फिर से शुरू है बूलियन isLoaded - लेकिन यह सबसे अच्छा समाधान नहीं है)।

उत्तर

12

एक अच्छा उदाहरण देखें वहाँ एक विधि है: public void onViewStateRestored (Bundle savedInstanceState)

जो onStart() से पहले कहा जाता है और onActivityCreated() के बाद docs में उल्लिखित।

एंड्रॉइड एपीआई < 17 में ऐसी कोई विधि नहीं है।

  1. जबकि Fragment आरंभ दृश्य राज्य पर भरोसा करते हैं और Fragment राज्य के रूप में सभी आवश्यक प्रारंभ राज्य सेव न करें (अर्थात ओवरराइड Fragment#onSaveInstanceState()): लेकिन वहाँ दो समाधान हैं। बाद में आप onCreate(), onCreateView() या onViewCreated() में खंड स्थिति को पुनर्स्थापित कर सकते हैं।
  2. प्रश्न में निर्दिष्ट अनुसार onStart() में प्रारंभिक प्रदर्शन करें।
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 - - - - - - -]

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

स्रोत: व्यावसायिक Android विकास 4 - रेटो मेयर

+0

'ऑनएटैच' दृश्य में मौजूद नहीं है। जैसा कि 'ऑनएक्टिविटीक्रेटेड' दृश्य में आपकी टिप्पणी में निर्दिष्ट है, लेकिन सहेजा गया दृश्य दृश्य स्थिति अभी तक पुनर्स्थापित नहीं किया गया है। – acc15

+0

हां व्यू क्रिएटिव्यू पर बनाया गया है। – Talha

+0

लेआउट व्यू ऑनक्रेट व्यू से लौटाया गया प्रारंभिक मान नहीं है और प्रारंभिक मान शामिल हैं। जब अभिविन्यास में परिवर्तन होता है तो मुझे उस समय बहाल मूल्यों की आवश्यकता होती है। – acc15

संबंधित मुद्दे