2012-09-28 12 views
97

बिल्कुल तब होता है जब आप को Fragment पर कॉल करते हैं? दस्तावेज लगभग मौजूद नहीं है और यह एक बहुत ही महत्वपूर्ण कार्य की तरह लगता है। विशेष रूप से मुझे पता है (है कि मैं बना हुआ) इस क्रम का कितना चाहते सत्य है:आगे की समझ सेट रीटेन इंस्टेंस (सत्य)

  1. उपयोगकर्ता डिवाइस घूमता है।
  2. टुकड़ा Activity और Fragment.onDetach() से अलग किया गया है।
  3. गतिविधि नष्ट हो गई है; Activity.onDestroy() कहा जाता है।
  4. Activity जावा ऑब्जेक्ट हटा दिया गया है (जब संभव हो, जीसी द्वारा)।
  5. एक नया Activity जावा ऑब्जेक्ट बनाया गया है; इसके कन्स्ट्रक्टर, और onCreate() कहा जाता है।
  6. Activity.onCreate() में हमारे पास setContentView(...) है जो एक टुकड़ा युक्त एक लेआउट सेट करता है, या हम एक टुकड़ा जोड़ने के लिए FragmentTransaction का उपयोग करते हैं।
  7. मैं वास्तव में इस बारे में निश्चित नहीं हूँ, लेकिन मुझे लगता है कि एंड्रॉयड बहुत चालाक पुराने टुकड़ा मिल जाए, और को Fragment.onAttach() फोन नई Activity
  8. के बगल में, यह फिर से अनुलग्न है (या पहले? कौन जानता है?) Activity.onResume() कहा जाता है।

तो क्या यह सही है? एंड्रॉइड स्मार्ट पुराने पुराने टुकड़े को खोजने के लिए पर्याप्त है, भले ही मैं पहली बार FragmentTransaction.add(new MyFragment(), ...) का उपयोग करता हूं? और यदि हां, तो में अन्य खंड जोड़ने से मैं कैसे बचूं? मैं इस ?: तरह Fragment कक्षा में

उत्तर

86

ठीक है, शायद मैं एंड्रॉइड प्रलेखन पर थोड़ा कठोर था, क्योंकि इसमें कुछ उपयोगी जानकारी है, लेकिन दुख की बात है कि इसमें से कोई भी setRetainInstance() से जुड़ा हुआ नहीं है। से the page about fragments

नोट: प्रत्येक टुकड़ा एक अद्वितीय पहचानकर्ता है कि सिस्टम उपयोग निकालने के रूप में अगर गतिविधि पुनरारंभ टुकड़ा बहाल कर सकते हैं (और जो आप टुकड़ा लेनदेन प्रदर्शन करने पर कब्जा करने के लिए उपयोग कर सकते हैं, जैसे की आवश्यकता है यह)। एक टुकड़े के लिए आईडी प्रदान करने के तीन तरीके हैं:

  • एंड्रॉइड की आपूर्ति करें: एक अद्वितीय आईडी के साथ आईडी विशेषता।
  • एंड्रॉइड की आपूर्ति: एक अद्वितीय स्ट्रिंग के साथ टैग विशेषता।
  • यदि आप पिछले दो में से कोई भी प्रदान नहीं करते हैं, तो सिस्टम कंटेनर व्यू की आईडी का उपयोग करता है।

यह दृढ़ता से संकेत मिलता है कि अगर आप Activity.onCreated() में setContentView(R.layout.whatever) करते हैं और है कि लेआउट setRetainInstance(true) साथ एक टुकड़ा होता है, तो जब गतिविधि से निर्मित किया गया है इसे फिर से अपनी आईडी या टैग का उपयोग कर के लिए खोज की जाएगी।

दूसरे, यूआई-कम टुकड़े के लिए, यह

एक यूआई के बिना एक टुकड़ा जोड़ने के लिए, जोड़ने का उपयोग कर गतिविधि से टुकड़ा जोड़ने के राज्यों (टुकड़ा, स्ट्रिंग) (एक अद्वितीय स्ट्रिंग की आपूर्ति "टैग" एक दृश्य आईडी के बजाय खंड के लिए)। यह खंड जोड़ता है, लेकिन, क्योंकि यह गतिविधि लेआउट में किसी दृश्य से संबद्ध नहीं है, यह पर CreateView() पर कॉल प्राप्त नहीं करता है। इसलिए आपको विधि को लागू करने की आवश्यकता नहीं है।

और दस्तावेज़ बहुत अच्छे उदाहरण से लिंक करते हैं - FragmentRetainInstance.java जो मैंने आपकी सुविधा के लिए नीचे पुन: प्रस्तुत किया है। यह वही करता है जो मैंने अनुमान लगाया था मेरे प्रश्न में उत्तर था (if (...findFragmentByTag() == null) { ...)।

अंत में, मैंने यह देखने के लिए अपनी खुद की परीक्षण गतिविधि बनाई कि वास्तव में क्या कार्य कहा जाता है। जब आप पोर्ट्रेट में शुरू होते हैं और परिदृश्य में घूमते हैं तो यह आउटपुट करता है। कोड नीचे है।

(यह यह पढ़ने में आसान बनाने के लिए एक सा संपादित किया जाता है।)

[email protected]: this() 
[email protected]: onCreate() 
[email protected]: Existing fragment not found. 
TestFragment{41583008}: this() TestFragment{41583008} 
TestFragment{41583008}: onAttach([email protected]) 
TestFragment{41583008}: onCreate() 
TestFragment{41583008}: onCreateView() 
TestFragment{41583008}: onActivityCreated() 
[email protected]: onStart() 
TestFragment{41583008}: onStart() 
[email protected]: onResume() 
TestFragment{41583008}: onResume() 

<rotate device> 

TestFragment{41583008}: onPause() 
[email protected]: onPause() 
TestFragment{41583008}: onStop() 
[email protected]: onStop() 
TestFragment{41583008}: onDestroyView() 
TestFragment{41583008}: onDetach() 
[email protected]: onDestroy() 
[email protected]: this() 
TestFragment{41583008}: onAttach([email protected]) 
[email protected]: onCreate() 
[email protected]: Existing fragment found. 
TestFragment{41583008}: onCreateView() 
TestFragment{41583008}: onActivityCreated() 
[email protected]: onStart() 
TestFragment{41583008}: onStart() 
[email protected]: onResume() 
TestFragment{41583008}: onResume() 

ध्यान दें कि Android दस्तावेज़ गलत है: यूआई-कम टुकड़ा onCreateView() को एक कॉल प्राप्त करता है, लेकिन यह है null वापस करने के लिए स्वतंत्र।

स्रोत FragmentRetainInstance.java के लिए TestActivity/TestFragment

import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import com.concentriclivers.ss.R; 

// An activity for understanding Android lifecycle events. 
public class TestActivity extends Activity 
{ 
    private static final String TAG = TestActivity.class.getSimpleName(); 

    public TestActivity() 
    { 
     super(); 
     Log.d(TAG, this + ": this()"); 
    } 

    protected void finalize() throws Throwable 
    { 
     super.finalize(); 
     Log.d(TAG, this + ": finalize()"); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     Log.d(TAG, this + ": onCreate()"); 


     TextView tv = new TextView(this); 
     tv.setText("Hello world"); 
     setContentView(tv); 

     if (getFragmentManager().findFragmentByTag("test_fragment") == null) 
     { 
      Log.d(TAG, this + ": Existing fragment not found."); 
      FragmentTransaction ft = getFragmentManager().beginTransaction(); 
      ft.add(new TestFragment(), "test_fragment").commit(); 
     } 
     else 
     { 
      Log.d(TAG, this + ": Existing fragment found."); 
     } 
    } 

    @Override 
    public void onStart() 
    { 
     super.onStart(); 
     Log.d(TAG, this + ": onStart()"); 
    } 

    @Override 
    public void onResume() 
    { 
     super.onResume(); 
     Log.d(TAG, this + ": onResume()"); 
    } 

    @Override 
    public void onPause() 
    { 
     super.onPause(); 
     Log.d(TAG, this + ": onPause()"); 
    } 

    @Override 
    public void onStop() 
    { 
     super.onStop(); 
     Log.d(TAG, this + ": onStop()"); 
    } 

    @Override 
    public void onDestroy() 
    { 
     super.onDestroy(); 
     Log.d(TAG, this + ": onDestroy()"); 
    } 




    public static class TestFragment extends Fragment 
    { 
     private static final String TAG = TestFragment.class.getSimpleName(); 

     public TestFragment() 
     { 
      super(); 
      Log.d(TAG, this + ": this() " + this); 
     } 

     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      Log.d(TAG, this + ": onCreate()"); 
      setRetainInstance(true); 
     } 

     @Override 
     public void onAttach(final Activity activity) 
     { 
      super.onAttach(activity); 
      Log.d(TAG, this + ": onAttach(" + activity + ")"); 
     } 

     @Override 
     public void onActivityCreated(Bundle savedInstanceState) 
     { 
      super.onActivityCreated(savedInstanceState); 
      Log.d(TAG, this + ": onActivityCreated()"); 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
     { 
      Log.d(TAG, this + ": onCreateView()"); 
      return null; 
     } 

     @Override 
     public void onViewCreated(View view, Bundle savedInstanceState) 
     { 
      super.onViewCreated(view, savedInstanceState); 
      Log.d(TAG, this + ": onViewCreated()"); 
     } 

     @Override 
     public void onDestroyView() 
     { 
      super.onDestroyView(); 
      Log.d(TAG, this + ": onDestroyView()"); 
     } 

     @Override 
     public void onDetach() 
     { 
      super.onDetach(); 
      Log.d(TAG, this + ": onDetach()"); 
     } 

     @Override 
     public void onStart() 
     { 
      super.onStart(); 
      Log.d(TAG, this + ": onStart()"); 
     } 

     @Override 
     public void onResume() 
     { 
      super.onResume(); 
      Log.d(TAG, this + ": onResume()"); 
     } 

     @Override 
     public void onPause() 
     { 
      super.onPause(); 
      Log.d(TAG, this + ": onPause()"); 
     } 

     @Override 
     public void onStop() 
     { 
      super.onStop(); 
      Log.d(TAG, this + ": onStop()"); 
     } 

     @Override 
     public void onDestroy() 
     { 
      super.onDestroy(); 
      Log.d(TAG, this + ": onDestroy()"); 
     } 
    } 

} 

स्रोत कोड के लिए कोड (एपीआई 16) के रूप में:

/* 
* Copyright (C) 2010 The Android Open Source Project 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 

package com.example.android.apis.app; 

import com.example.android.apis.R; 

import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ProgressBar; 

/** 
* This example shows how you can use a Fragment to easily propagate state 
* (such as threads) across activity instances when an activity needs to be 
* restarted due to, for example, a configuration change. This is a lot 
* easier than using the raw Activity.onRetainNonConfiguratinInstance() API. 
*/ 
public class FragmentRetainInstance extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // First time init, create the UI. 
     if (savedInstanceState == null) { 
      getFragmentManager().beginTransaction().add(android.R.id.content, 
        new UiFragment()).commit(); 
     } 
    } 

    /** 
    * This is a fragment showing UI that will be updated from work done 
    * in the retained fragment. 
    */ 
    public static class UiFragment extends Fragment { 
     RetainedFragment mWorkFragment; 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View v = inflater.inflate(R.layout.fragment_retain_instance, container, false); 

      // Watch for button clicks. 
      Button button = (Button)v.findViewById(R.id.restart); 
      button.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        mWorkFragment.restart(); 
       } 
      }); 

      return v; 
     } 

     @Override 
     public void onActivityCreated(Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState); 

      FragmentManager fm = getFragmentManager(); 

      // Check to see if we have retained the worker fragment. 
      mWorkFragment = (RetainedFragment)fm.findFragmentByTag("work"); 

      // If not retained (or first time running), we need to create it. 
      if (mWorkFragment == null) { 
       mWorkFragment = new RetainedFragment(); 
       // Tell it who it is working with. 
       mWorkFragment.setTargetFragment(this, 0); 
       fm.beginTransaction().add(mWorkFragment, "work").commit(); 
      } 
     } 

    } 

    /** 
    * This is the Fragment implementation that will be retained across 
    * activity instances. It represents some ongoing work, here a thread 
    * we have that sits around incrementing a progress indicator. 
    */ 
    public static class RetainedFragment extends Fragment { 
     ProgressBar mProgressBar; 
     int mPosition; 
     boolean mReady = false; 
     boolean mQuiting = false; 

     /** 
     * This is the thread that will do our work. It sits in a loop running 
     * the progress up until it has reached the top, then stops and waits. 
     */ 
     final Thread mThread = new Thread() { 
      @Override 
      public void run() { 
       // We'll figure the real value out later. 
       int max = 10000; 

       // This thread runs almost forever. 
       while (true) { 

        // Update our shared state with the UI. 
        synchronized (this) { 
         // Our thread is stopped if the UI is not ready 
         // or it has completed its work. 
         while (!mReady || mPosition >= max) { 
          if (mQuiting) { 
           return; 
          } 
          try { 
           wait(); 
          } catch (InterruptedException e) { 
          } 
         } 

         // Now update the progress. Note it is important that 
         // we touch the progress bar with the lock held, so it 
         // doesn't disappear on us. 
         mPosition++; 
         max = mProgressBar.getMax(); 
         mProgressBar.setProgress(mPosition); 
        } 

        // Normally we would be doing some work, but put a kludge 
        // here to pretend like we are. 
        synchronized (this) { 
         try { 
          wait(50); 
         } catch (InterruptedException e) { 
         } 
        } 
       } 
      } 
     }; 

     /** 
     * Fragment initialization. We way we want to be retained and 
     * start our thread. 
     */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      // Tell the framework to try to keep this fragment around 
      // during a configuration change. 
      setRetainInstance(true); 

      // Start up the worker thread. 
      mThread.start(); 
     } 

     /** 
     * This is called when the Fragment's Activity is ready to go, after 
     * its content view has been installed; it is called both after 
     * the initial fragment creation and after the fragment is re-attached 
     * to a new activity. 
     */ 
     @Override 
     public void onActivityCreated(Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState); 

      // Retrieve the progress bar from the target's view hierarchy. 
      mProgressBar = (ProgressBar)getTargetFragment().getView().findViewById(
        R.id.progress_horizontal); 

      // We are ready for our thread to go. 
      synchronized (mThread) { 
       mReady = true; 
       mThread.notify(); 
      } 
     } 

     /** 
     * This is called when the fragment is going away. It is NOT called 
     * when the fragment is being propagated between activity instances. 
     */ 
     @Override 
     public void onDestroy() { 
      // Make the thread go away. 
      synchronized (mThread) { 
       mReady = false; 
       mQuiting = true; 
       mThread.notify(); 
      } 

      super.onDestroy(); 
     } 

     /** 
     * This is called right before the fragment is detached from its 
     * current activity instance. 
     */ 
     @Override 
     public void onDetach() { 
      // This fragment is being detached from its activity. We need 
      // to make sure its thread is not going to touch any activity 
      // state after returning from this function. 
      synchronized (mThread) { 
       mProgressBar = null; 
       mReady = false; 
       mThread.notify(); 
      } 

      super.onDetach(); 
     } 

     /** 
     * API for our UI to restart the progress thread. 
     */ 
     public void restart() { 
      synchronized (mThread) { 
       mPosition = 0; 
       mThread.notify(); 
      } 
     } 
    } 
} 
+0

क्या आप मेरे प्रश्न को देखकर मुझे बताएंगे कि मैं आपके समाधान को कैसे कार्यान्वित करूंगा। मेरा ऐप काम करता है लेकिन मैं एंड्रॉइड को हटाना चाहता हूं: configChanges = "orientation | keyboardHidden | screenSize" और इसके परिणामस्वरूप ऐप कॉन्फ़िगरेशन परिवर्तन के बाद अपने फ्रैगमेंट श्रोता को बनाए रखने में असफल रहा। http://stackoverflow.com/questions/35941585/avoiding-manually-handling-configuration-changes –

40

setRetainInstance() कुछ करने की जरूरत है Activity वर्ग के onRetainCustomNonConfigurationInstance(), और अधिक के लिए एक चतुर प्रतिस्थापन है।

स्पष्ट रूप से in the documentation कहा गया।

डिफ़ॉल्ट setRetainInstance(false)

09-29 13:23:04.771: DEBUG/szipinf(4790): Initializing inflate state 
09-29 13:23:04.801: INFO/TESTING - MYACTIVITY(4790): Instantiated - [email protected] 
09-29 13:23:04.851: DEBUG/dalvikvm(4790): GC_EXTERNAL_ALLOC freed 49K, 51% free 2686K/5379K, external 0K/0K, paused 45ms 
09-29 13:23:04.881: INFO/TESTING - MYACTIVITY(4790): onCreate - [email protected] 
09-29 13:23:04.881: INFO/TESTING - MYACTIVITY(4790): onStart - [email protected] 
09-29 13:23:04.881: INFO/TESTING - MYACTIVITY(4790): onResume - [email protected] 
09-29 13:23:04.891: INFO/TESTING - MYACTIVITY(4790): onAttachedToWindow - [email protected] 
09-29 13:23:10.381: DEBUG/dalvikvm(4457): GC_EXPLICIT freed 8K, 51% free 2681K/5379K, external 0K/0K, paused 38ms 
09-29 13:23:11.901: INFO/TESTING - MYFRAGMENT(4790): Instantiated - MyFragment{40530610} 
09-29 13:23:11.911: INFO/TESTING - MYFRAGMENT(4790): onAttach - MyFragment{40530610 #0 MyFragment} 
09-29 13:23:11.911: INFO/TESTING - MYACTIVITY(4790): onAttachFragment - [email protected] 
09-29 13:23:11.911: INFO/TESTING - MYFRAGMENT(4790): onCreate - MyFragment{40530610 #0 MyFragment} 
09-29 13:23:11.911: INFO/TESTING - MYFRAGMENT(4790): onCreateView - MyFragment{40530610 #0 MyFragment} 
09-29 13:23:11.921: INFO/TESTING - MYFRAGMENT(4790): onViewCreated - MyFragment{40530610 #0 MyFragment} 
09-29 13:23:11.921: INFO/TESTING - MYFRAGMENT(4790): onActivityCreated - MyFragment{40530610 #0 MyFragment} 
09-29 13:23:11.931: INFO/TESTING - MYFRAGMENT(4790): onStart - MyFragment{40530610 #0 MyFragment} 
09-29 13:23:11.931: INFO/TESTING - MYFRAGMENT(4790): onResume - MyFragment{40530610 #0 MyFragment} 
09-29 13:23:15.081: INFO/ActivityManager(1268): Config changed: { scale=1.0 imsi=404/45 loc=en_US touch=3 keys=1/1/2 nav=1/1 orien=2 layout=34 uiMode=17 seq=105 themeResource=null} 
09-29 13:23:15.111: INFO/TESTING - MYACTIVITY(4790): onSaveInstanceState - [email protected] 
09-29 13:23:15.111: INFO/TESTING - MYFRAGMENT(4790): onPause - MyFragment{40530610 #0 MyFragment} 
09-29 13:23:15.121: INFO/TESTING - MYACTIVITY(4790): onPause - [email protected] 
09-29 13:23:15.121: INFO/TESTING - MYFRAGMENT(4790): onStop - MyFragment{40530610 #0 MyFragment} 
09-29 13:23:15.121: INFO/TESTING - MYACTIVITY(4790): onStop - [email protected] 
09-29 13:23:15.121: INFO/TESTING - MYACTIVITY(4790): onRetainCustomNonConfigurationInstance - [email protected] 
09-29 13:23:15.121: INFO/TESTING - MYFRAGMENT(4790): onDestroyView - MyFragment{40530610 #0 MyFragment} 
09-29 13:23:15.121: INFO/TESTING - MYFRAGMENT(4790): onDestroy - MyFragment{40530610 #0 MyFragment} 
09-29 13:23:15.121: INFO/TESTING - MYFRAGMENT(4790): onDetach - MyFragment{40530610 #0 MyFragment} 
09-29 13:23:15.121: INFO/TESTING - MYACTIVITY(4790): onDestroy - [email protected] 
09-29 13:23:15.191: INFO/TESTING - MYACTIVITY(4790): onDetachedFromWindow - [email protected] 
09-29 13:23:15.201: INFO/TESTING - MYACTIVITY(4790): Instantiated - [email protected] 
09-29 13:23:15.201: INFO/TESTING - MYFRAGMENT(4790): Instantiated - MyFragment{4053cde0} 
09-29 13:23:15.201: INFO/TESTING - MYFRAGMENT(4790): onAttach - MyFragment{4053cde0 #0 MyFragment} 
09-29 13:23:15.201: INFO/TESTING - MYACTIVITY(4790): onAttachFragment - [email protected] 
09-29 13:23:15.201: INFO/TESTING - MYFRAGMENT(4790): onCreate - MyFragment{4053cde0 #0 MyFragment} 
09-29 13:23:15.251: INFO/TESTING - MYACTIVITY(4790): onCreate - [email protected] 
09-29 13:23:15.251: INFO/TESTING - MYFRAGMENT(4790): onCreateView - MyFragment{4053cde0 #0 MyFragment} 
09-29 13:23:15.261: INFO/TESTING - MYFRAGMENT(4790): onViewCreated - MyFragment{4053cde0 #0 MyFragment} 
09-29 13:23:15.261: INFO/TESTING - MYFRAGMENT(4790): onActivityCreated - MyFragment{4053cde0 #0 MyFragment} 
09-29 13:23:15.291: INFO/TESTING - MYFRAGMENT(4790): onStart - MyFragment{4053cde0 #0 MyFragment} 
09-29 13:23:15.291: INFO/TESTING - MYACTIVITY(4790): onStart - [email protected] 
09-29 13:23:15.291: INFO/TESTING - MYACTIVITY(4790): onResume - [email protected] 
09-29 13:23:15.291: INFO/TESTING - MYFRAGMENT(4790): onResume - MyFragment{4053cde0 #0 MyFragment} 
09-29 13:23:15.321: INFO/TESTING - MYACTIVITY(4790): onAttachedToWindow - [email protected] 

तो, टुकड़ा पूरी नई निर्मित है, और पता चला फिर,:

यह इस प्रकार होता की प्रवेश (ए यूआई टुकड़ा मांग और फिर एक config परिवर्तन पर जोड़ें) है यह सब करते हुए setRetainInstance(false)

और अब setRetainInstance(true)

साथ
09-29 13:18:46.121: INFO/ActivityManager(1268): Starting: Intent { flg=0x10100000 cmp=com.example/.MyActivity } from pid 1268 
09-29 13:18:46.141: INFO/TESTING - MYACTIVITY(4726): Instantiated - [email protected] 
09-29 13:18:46.161: INFO/TESTING - MYACTIVITY(4726): onCreate - [email protected] 
09-29 13:18:46.161: INFO/TESTING - MYACTIVITY(4726): onStart - [email protected] 
09-29 13:18:46.161: INFO/TESTING - MYACTIVITY(4726): onResume - [email protected] 
09-29 13:18:46.191: INFO/TESTING - MYACTIVITY(4726): onAttachedToWindow - [email protected] 
09-29 13:19:10.431: DEBUG/SntpClient(1268): request time failed: java.net.UnknownHostException: europe.pool.ntp.org 
09-29 13:19:14.251: INFO/TESTING - MYFRAGMENT(4726): Instantiated - MyFragment{405288c0} 
09-29 13:19:14.271: INFO/TESTING - MYFRAGMENT(4726): onAttach - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:14.271: INFO/TESTING - MYACTIVITY(4726): onAttachFragment - [email protected] 
09-29 13:19:14.271: INFO/TESTING - MYFRAGMENT(4726): onCreate - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:14.281: INFO/TESTING - MYFRAGMENT(4726): onCreateView - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:14.281: INFO/TESTING - MYFRAGMENT(4726): onViewCreated - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:14.281: INFO/TESTING - MYFRAGMENT(4726): onActivityCreated - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:14.291: INFO/TESTING - MYFRAGMENT(4726): onStart - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:14.291: INFO/TESTING - MYFRAGMENT(4726): onResume - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:21.921: INFO/ActivityManager(1268): Config changed: { scale=1.0 imsi=404/45 loc=en_US touch=3 keys=1/1/2 nav=1/1 orien=2 layout=34 uiMode=17 seq=103 themeResource=null} 
09-29 13:19:21.961: INFO/TESTING - MYACTIVITY(4726): onSaveInstanceState - [email protected] 
09-29 13:19:21.961: INFO/TESTING - MYFRAGMENT(4726): onPause - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:21.961: INFO/TESTING - MYACTIVITY(4726): onPause - [email protected] 
09-29 13:19:21.961: INFO/TESTING - MYFRAGMENT(4726): onStop - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:21.961: INFO/TESTING - MYACTIVITY(4726): onStop - [email protected] 
09-29 13:19:21.961: INFO/TESTING - MYACTIVITY(4726): onRetainCustomNonConfigurationInstance - [email protected] 
09-29 13:19:21.961: INFO/TESTING - MYFRAGMENT(4726): onDestroyView - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:21.961: INFO/TESTING - MYFRAGMENT(4726): onDetach - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:21.961: INFO/TESTING - MYACTIVITY(4726): onDestroy - [email protected] 
09-29 13:19:22.111: INFO/TESTING - MYACTIVITY(4726): onDetachedFromWindow - [email protected] 
09-29 13:19:22.111: INFO/TESTING - MYACTIVITY(4726): Instantiated - [email protected] 
09-29 13:19:22.111: INFO/TESTING - MYFRAGMENT(4726): onAttach - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:22.111: INFO/TESTING - MYACTIVITY(4726): onAttachFragment - [email protected] 
09-29 13:19:22.131: INFO/TESTING - MYACTIVITY(4726): onCreate - [email protected] 
09-29 13:19:22.131: INFO/TESTING - MYFRAGMENT(4726): onCreateView - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:22.131: INFO/TESTING - MYFRAGMENT(4726): onViewCreated - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:22.131: INFO/TESTING - MYFRAGMENT(4726): onActivityCreated - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:22.141: INFO/TESTING - MYFRAGMENT(4726): onStart - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:22.141: INFO/TESTING - MYACTIVITY(4726): onStart - [email protected] 
09-29 13:19:22.141: INFO/TESTING - MYACTIVITY(4726): onResume - [email protected] 
09-29 13:19:22.141: INFO/TESTING - MYFRAGMENT(4726): onResume - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:22.171: INFO/TESTING - MYACTIVITY(4726): onAttachedToWindow - [email protected] 
09-29 13:19:22.181: INFO/TESTING - MYFRAGMENT(4726): onPause - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:22.181: INFO/TESTING - MYFRAGMENT(4726): onStop - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:22.181: INFO/TESTING - MYFRAGMENT(4726): onDestroyView - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:22.181: INFO/TESTING - MYFRAGMENT(4726): onDestroy - MyFragment{405288c0 #0 MyFragment} 
09-29 13:19:22.181: INFO/TESTING - MYFRAGMENT(4726): onDetach - MyFragment{405288c0 #0 MyFragment} 

प्रभाव को नोटिस किया गया? टुकड़ा उदाहरण (ऑब्जेक्ट 405288 सी 0) बनाए रखा गया था जो अच्छा है। लेकिन बनाए रखा उदाहरण संसाधनों और विचारों और वस्तुओं को पकड़ने की संभावना है जो पिछली ओरिएंटेशन से संबंधित हैं, जो स्मृति रिसाव का कारण बन सकती हैं।

आगे की देखभाल की जानी चाहिए जहां आप इस खंड को शुरू करने के लिए कोड लिखते हैं: आपको हमेशा पूर्व-मौजूदा उदाहरण की जांच करनी होगी।

कहानी का नैतिक: setRetainInstance() गैर दृश्य टुकड़ों के लिए सबसे अच्छा उपयोग किया जाता है।

+0

दो लॉग मुझे जवाब दे .. सबसे पहले लॉग हर बार नया उदाहरण बनाने (40530610,4053cde0), setRetainInstance() के बाद केवल एक उदाहरण 405288c0 –

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