6

मैं एक कस्टम दृश्य है, मान लें कि यह अपने कोड है:एक्सएमएल लागू करने से पहले देखने का राज्य पुनर्स्थापित विशेषताओं

public class CustomView extends View { 

    boolean visible; 
    boolean enabled; 

    public ScheduleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 

     TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0); 
     try { 
      visible = a.getBoolean(R.styleable.CustomView_visible, true); 
      enabled = a.getBoolean(R.styleable.CustomView_enabled, true); 
     } finally { 
      a.recycle(); 
     } 

     // Apply XML attributes here 
    } 

    @Override 
    public Parcelable onSaveInstanceState() { 
     // Save instance state 
     Bundle bundle = new Bundle(); 
     bundle.putParcelable("superState", super.onSaveInstanceState()); 
     bundle.putBoolean("visible", visible); 
     bundle.putBoolean("enabled", enabled); 

     return bundle; 
    } 

    @Override 
    public void onRestoreInstanceState(Parcelable state) { 
     // Restore instance state 
     // This is called after constructor 
     if (state instanceof Bundle) { 
      Bundle bundle = (Bundle) state; 
      visible = bundle.getBoolean("visible"); 
      enabled = bundle.getBoolean("enabled"); 

      state = bundle.getParcelable("superState"); 
     } 
     super.onRestoreInstanceState(state); 
    } 
} 

सुंदर सरल। मेरा कस्टम व्यू एक्सएमएल से गुण पढ़ता है और उन्हें लागू करता है। इन विशेषताओं को कॉन्फ़िगरेशन परिवर्तनों पर सहेजा और बहाल किया जाता है।

लेकिन अगर मैं दो अलग झुकाव के लिए दो अलग अलग लेआउट, उदाहरण के लिए:

[layout-port/view.xml] 
<CustomView 
    custom:visible="true" 
    custom:enabled="true" 

[layout-land/view.xml] 
<CustomView 
    custom:visible="false" 
    custom:enabled="false" 

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

मैं कुछ गलत कर रहा हूँ? इसे हल करने का सबसे अच्छा तरीका क्या होगा?

+0

दुकान एक्सएमएल मूल्यों और बहाली के बाद उन्हें पुन: लागू। आप बहाली को भी लागू नहीं कर सकते हैं, इसलिए मान हमेशा xml – nandsito

+0

@nandsito में परिभाषित किए जाएंगे, शायद यह है कि मैं क्या कर रहा हूं। मैंने सोचा कि शायद ऐसा करने का एक और सीधा तरीका है, एक्सएमएल पार्स करने के बाद राज्य को बहाल करने का एक तरीका। मैंने सोचा कि मैं कर सकता हूं कि एट्रिब्यूटसेट को एक चर में सहेजना है, फिर RestoreInstanteState के अंत में XML को पार्स करें। लेकिन जब रीस्टोर इंस्टेंटस्टेट को पहली बार बनाया गया है तो कॉल नहीं किया जाता है। –

+0

एंड्रॉइड एक्सएमएल पार्स करता है और दृश्य कन्स्ट्रक्टर में इसके गुणों को लागू करता है, इसलिए एक्सएमएल हमेशा पुनर्स्थापित स्थिति से पहले संसाधित होता है। अगर आप इस ऑर्डर को बदलना चाहते हैं, तो आपको वैरिएबल वैल्यू मैन्युअल रूप से सेट करना होगा – nandsito

उत्तर

0

अपने मामले में आपको Parcelable में अन्य विशेषताओं के साथ वर्तमान अभिविन्यास को स्टोर करना होगा, और पुनर्स्थापित अभिविन्यास वर्तमान अभिविन्यास के बराबर है (यानी गतिविधि को नष्ट कर दिया गया था और ओएस द्वारा बहाल किया गया था)। आपके मामले में मैं इस तरह वर्तमान अभिविन्यास परिभाषित करने के लिए android:tag का प्रयोग करेंगे:

[layout-port/view.xml] 
<CustomView 
    android:tag="port" 
    custom:visible="true" 
    custom:enabled="true" 

[layout-land/view.xml] 
<CustomView 
    android:tag="land" 
    custom:visible="false" 
    custom:enabled="false" 

और फिर अपने कस्टम दृश्य वर्ग होगा जैसे:

public class ScheduleView extends View { 

    String orientation; 
    boolean visible; 
    boolean enabled; 

    public ScheduleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 

     TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0); 
     try { 
      visible = a.getBoolean(R.styleable.CustomView_visible, true); 
      enabled = a.getBoolean(R.styleable.CustomView_enabled, true); 
     } finally { 
      a.recycle(); 
     } 

     orientation = (String) getTag(); 
    } 

    @Override 
    public Parcelable onSaveInstanceState() { 
     // Save instance state 
     Bundle bundle = new Bundle(); 
     bundle.putParcelable("superState", super.onSaveInstanceState()); 
     bundle.putBoolean("visible", visible); 
     bundle.putBoolean("enabled", enabled); 
     bundle.putString("orientation", orientation); 

     return bundle; 
    } 

    @Override 
    public void onRestoreInstanceState(Parcelable state) { 
     // Restore instance state 
     // This is called after constructor 
     if (state instanceof Bundle) { 
      Bundle bundle = (Bundle) state; 

      String restoredOrientation = bundle.getString("orientation"); 
      if (restoredOrientation.equals(orientation)) { 
       visible = bundle.getBoolean("visible"); 
       enabled = bundle.getBoolean("enabled"); 
      } 

      state = bundle.getParcelable("superState"); 
     } 
     super.onRestoreInstanceState(state); 
    } 
} 

यह ठीक से जांच नहीं की है, लेकिन यह काम करना चाहिए। उम्मीद है कि यह सहायक होगा।

+0

मैं कुछ सहेजे गए विशेषताओं को लागू करना चाहता हूं, जो एक्सएमएल में नहीं हैं। एक ही बात अजीज़बेकियन के जवाब पर लागू होती है। –

+0

क्षमा करें, लेकिन मुझे आपको समस्या नहीं आती है। आप वास्तव में 'onRestoreInstanceState' विधि' में 'if' block के बाहर उन आउट-ऑफ-एक्सएमएल विशेषताओं को लागू कर सकते हैं। – rom4ek

+0

'onRestoreInstanceState' हमेशा यह नहीं कहा जाता है कि यह समस्या है। –

0

असल में, आपको राज्यों को चित्र और परिदृश्य में अलग करने की आवश्यकता है, जिसका अर्थ है कि आपको विशेष कॉन्फ़िगरेशन स्थिति भी सहेजनी होगी।

final boolean isPortrait = 
      getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT; 
bundle.putBoolean("isPortrait", isPortrait); 

फिर जब राज्य को बहाल: अन्य चर में

final boolean savedOrientation = bundle.getBoolean("isPortrait"); 
final boolean currentOrientation = 
      getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT; 

if (savedOrientation == currentOrientation) { 
    // now retrieve saved values 
} else { 
    // do nothing, values are initialized in constructor 
} 
संबंधित मुद्दे