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.
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() ).
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.
In this method, when you switch from Portrait to Landscape, a method is called, onConfigurationChanged method.
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 :
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With