Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Dialog ,"window leaked" issue when changing orientation

I have two classes one adapter and one activity , i have a dailog which i am showing in the adapter. i am getting error when i tried to change the screen orientation..i tried overriding the below code in my activity. but nothing seems to work

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
          Log.d(TAG, "configuration changed : " +newConfig);

    }

below is my adapter code

public AddressPopUpAdapter(Activity activity, Activity parent,
            int resourceId, ArrayList<PopUpMenu> items, int renderer) {
        super(activity, resourceId, items);
        this.items = items;
        this.renderer = renderer;
        this.activity = activity;
        this.parentActivity = parent;
        popupMenuUtils = new PopupMenuUtils();
        dialog = new Dialog(parentActivity);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        popUpMenu = items.get(position);
        Log.d(TAG, "location" + popUpMenu);
        if (popUpMenu == null) {
            return null;
        }
        Log.d(TAG, "location" + popUpMenu);
        View view = convertView;
        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater) (getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE));
            view = layoutInflater.inflate(renderer, null);

        }

        final LinearLayout popupmain = (LinearLayout) view
                .findViewById(R.id.popupmain);

        popupmain.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setEnabled(false);
                performAction(v, activity);

            }
        });

        if (position % 2 == 0) {

            view.setBackgroundResource(R.drawable.listshape);

        } else {
            view.setBackgroundResource(R.drawable.favoritebody);
        }
        return view;
    }

    public void performAction(View v, Activity activity) {

        Context myContext = v.getContext();
        PopUpMenu popUpMenu = (PopUpMenu) v.getTag();
        String result = popUpMenu.getMenuName();
        if (result != null
                && result.equalsIgnoreCase(myContext.getResources().getString(
                        R.string.savecurrentlocation))) {
            getCurrentLocation();

        }

        else if (result != null
                && result.equalsIgnoreCase(myContext.getResources().getString(
                        R.string.distancebetween))) {
            AttractionService service = AttractionService
                    .getInstance(parentActivity.getApplicationContext());
            ArrayList<AttractionData> allMenuSearchList = service
                    .getAllAttractions(true, Constants.field, Constants.order);

            if (allMenuSearchList != null && !allMenuSearchList.isEmpty()) {

                dialog.setContentView(R.layout.pickdestination);
                ListView listPlace = (ListView) dialog
                        .findViewById(R.id.listPlace);
                PlaceAdapter placeAdapter = new PlaceAdapter(parentActivity,
                        allMenuSearchList, R.layout.pickcity, dialog,
                        R.string.pickstartingpoint, null);
                listPlace.setAdapter(placeAdapter);
giving error here----------->   dialog.show();

            } else {
                Toast.makeText(parentActivity, R.string.nonavigationtolink,
                        Toast.LENGTH_SHORT).show();

            }

            this.activity.finish();
        }

Any help is appreciated.

like image 537
teekib Avatar asked Jan 31 '13 09:01

teekib


People also ask

What happens when screen orientation changes in android?

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and when the user enables multi-window mode). When such a change occurs, Android restarts the running Activity ( onDestroy() is called, followed by onCreate() ).

What class ensures the dialog is displayed properly even when the user changes the device orientation?

The DialogFragment class provides all the controls you need to create your dialog and manage its appearance, instead of calling methods on the Dialog object. Using DialogFragment to manage the dialog ensures that it correctly handles lifecycle events such as when the user presses the Back button or rotates the screen.

Which methods are called when the screen changes orientation from portrait to landscape in android?

In this method, when you switch from Portrait to Landscape, a method is called, onConfigurationChanged method.


2 Answers

Thing is that you are not dismiss() your Dialog and finishing the activity so when orientation change it will get prior Dialog state.

So try to dismiss() your dialog before you finish your activity.

See More Detail if any other cause :

like image 67
Chintan Khetiya Avatar answered Oct 16 '22 08:10

Chintan Khetiya


Per the above hints by chintan/Stephan; I made the AlertDialog reference global to the Activity and did dismiss in onDestroy():

if (ad!=null) {
 ad.dismiss();
 ad=null;
}

No more logged leak errors upon rotation.

I would not want to consider those configChanges parameters because they would mess things up if you already have specific layouts for landscape or such.

like image 20
Gunnar Forsgren - Mobimation Avatar answered Oct 16 '22 07:10

Gunnar Forsgren - Mobimation