गैर स्थैतिक आंतरिक वर्ग उनके मूल वर्गों का संदर्भ रखते हैं। एक टुकड़ा आंतरिक वर्ग गैर स्थैतिक बनाने में समस्या यह है कि आप हमेशा गतिविधि का संदर्भ रखते हैं। GarbageCollector आपकी गतिविधि एकत्र नहीं कर सकता है। तो यदि आप उदाहरण के लिए अभिविन्यास बदलते हैं तो आप गतिविधि 'रिसाव' कर सकते हैं। क्योंकि टुकड़ा अभी भी जीवित रह सकता है और एक नए गतिविधि में डाला जा सकता है।
संपादित करें:
के बाद से कुछ लोग मुझे कुछ उदाहरण मैं एक लेखन शुरू करने के लिए कहा है, जबकि यह कर मैं कुछ और अधिक समस्याओं जब गैर स्थिर टुकड़े का उपयोग कर पाया:
- वे में नहीं किया जा सकता एक एक्सएमएल फ़ाइल क्योंकि उनके पास खाली कन्स्ट्रक्टर नहीं है (उनके पास एक खाली कन्स्ट्रक्टर हो सकता है, लेकिन आप आमतौर पर
myActivityInstance.new Fragment()
करके गैरस्टिक घोंसला वाले वर्गों को तुरंत चालू करते हैं और यह केवल खाली कन्स्ट्रक्टर को कॉल करने के लिए अलग है)
- वे बी नहीं कर सकते ई बिल्कुल उपयोग किया गया - चूंकि
FragmentManager
कभी-कभी इस खाली कन्स्ट्रक्टर को भी कॉल करता है। यदि आपने कुछ लेनदेन में फ्रैगमेंट जोड़ा है।
तो आदेश मेरे उदाहरण काम करने के लिए में मैं
wrongFragment.setRetainInstance(true);
लाइन को जोड़ने के लिए उन्मुखीकरण परिवर्तन पर ऐप्लिकेशन क्रैश नहीं किया था।
यदि आप इस कोड को निष्पादित करते हैं तो आपके पास कुछ टेक्स्टव्यू और 2 बटन के साथ एक गतिविधि होगी - बटन कुछ काउंटर बढ़ाते हैं। और टुकड़े उन्मुखीकरण को दिखाते हैं जो उन्हें लगता है कि उनकी गतिविधि है। शुरुआत में सब ठीक से काम करता है। लेकिन स्क्रीन अभिविन्यास को बदलने के बाद केवल पहला फ्रैगमेंट काम करता है - दूसरा अभी भी अपनी पुरानी गतिविधि में सामान बुला रहा है।
मेरी गतिविधि वर्ग:
package com.example.fragmenttest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class WrongFragmentUsageActivity extends Activity
{
private String mActivityOrientation="";
private int mButtonClicks=0;
private TextView mClickTextView;
private static final String WRONG_FRAGMENT_TAG = "WrongFragment" ;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE)
{
mActivityOrientation = "Landscape";
}
else if (orientation == Configuration.ORIENTATION_PORTRAIT)
{
mActivityOrientation = "Portrait";
}
setContentView(R.layout.activity_wrong_fragement_usage);
mClickTextView = (TextView) findViewById(R.id.clicksText);
updateClickTextView();
TextView orientationtextView = (TextView) findViewById(R.id.orientationText);
orientationtextView.setText("Activity orientation is: " + mActivityOrientation);
Fragment wrongFragment = (WrongFragment) getFragmentManager().findFragmentByTag(WRONG_FRAGMENT_TAG);
if (wrongFragment == null)
{
wrongFragment = new WrongFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.mainView, wrongFragment, WRONG_FRAGMENT_TAG);
ft.commit();
wrongFragment.setRetainInstance(true); // <-- this is important - otherwise the fragment manager will crash when readding the fragment
}
}
private void updateClickTextView()
{
mClickTextView.setText("The buttons have been pressed " + mButtonClicks + " times");
}
private String getActivityOrientationString()
{
return mActivityOrientation;
}
@SuppressLint("ValidFragment")
public class WrongFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
LinearLayout result = new LinearLayout(WrongFragmentUsageActivity.this);
result.setOrientation(LinearLayout.VERTICAL);
Button b = new Button(WrongFragmentUsageActivity.this);
b.setText("WrongFragmentButton");
result.addView(b);
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
buttonPressed();
}
});
TextView orientationText = new TextView(WrongFragmentUsageActivity.this);
orientationText.setText("WrongFragment Activities Orientation: " + getActivityOrientationString());
result.addView(orientationText);
return result;
}
}
public static class CorrectFragment extends Fragment
{
private WrongFragmentUsageActivity mActivity;
@Override
public void onAttach(Activity activity)
{
if (activity instanceof WrongFragmentUsageActivity)
{
mActivity = (WrongFragmentUsageActivity) activity;
}
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
LinearLayout result = new LinearLayout(mActivity);
result.setOrientation(LinearLayout.VERTICAL);
Button b = new Button(mActivity);
b.setText("CorrectFragmentButton");
result.addView(b);
b.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
mActivity.buttonPressed();
}
});
TextView orientationText = new TextView(mActivity);
orientationText.setText("CorrectFragment Activities Orientation: " + mActivity.getActivityOrientationString());
result.addView(orientationText);
return result;
}
}
public void buttonPressed()
{
mButtonClicks++;
updateClickTextView();
}
}
ध्यान दें कि आप शायद onAttach
में गतिविधि डाली नहीं करना चाहिए आप विभिन्न गतिविधियों में अपने टुकड़ा उपयोग करना चाहते हैं - लेकिन यहाँ के लिए उदाहरण के लिए अपने काम कर रहे।
activity_wrong_fragement_usage.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".WrongFragmentUsageActivity"
android:id="@+id/mainView">
<TextView
android:id="@+id/orientationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/clicksText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<fragment class="com.example.fragmenttest.WrongFragmentUsageActivity$CorrectFragment"
android:id="@+id/correctfragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
स्रोत
2013-03-22 17:19:55
फाहा अनदेखी करने के लिए सामान्य अपने बुरे व्यवहार में कोड – rkmax
दिखा सकते हैं। यह एक बहुत ही स्मार्ट उपकरण है। अपने कोड को पोस्ट करने का प्रयास करें, वास्तव में यह जानने के लिए कि आप बेहतर काम कैसे कर सकते हैं। –
क्या आपने यह जानने के लिए यह http://code.google.com/p/android/issues/detail?id=41800 चेक किया है कि ValidFragment के बारे में है? लिंट कहती है कि: 'प्रत्येक टुकड़े में एक खाली कन्स्ट्रक्टर होना चाहिए, इसलिए इसे तत्काल' – sandrstar