Reputation: 876
I am using a popupmenu like below;
popupMenu = PopupMenu(view.context,view);
val menuInflater:MenuInflater = popupMenu!!.menuInflater;
menuInflater.inflate(R.menu.profil_image_degistir_menu,popupMenu!!.menu);
this.profilImage = profilImage as ImageView;
popupMenu!!.setOnMenuItemClickListener(object:PopupMenu.OnMenuItemClickListener{
override fun onMenuItemClick(item: MenuItem?): Boolean {
if(item?.itemId == R.id.action_cam){
camContext = view.context;
kameraIzin = ContextCompat.checkSelfPermission(view.context,Manifest.permission.CAMERA);
if(kameraIzin != PackageManager.PERMISSION_GRANTED){
requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE);
}else{
val intent: Intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CAMERA_REQUEST_CODE);
}
}else if(item?.itemId == R.id.action_localStorage){
}
return true;
}
});
popupMenu!!.show();
But when I rotate the screen menu is closing? How to prevent this?Can u help me.
Upvotes: 1
Views: 335
Reputation: 40878
In manifest file you need to add android:configChanges="orientation"
in the activity that hosts this popup menu.
Upvotes: 0
Reputation: 584
When you rotate the device a configuration change takes place; this causes the activity to be recreated again. To control configuration changes you should use the onSaveInstanceState
method. This method execute when a configuration change occur. In this case you should save a boolean value indicating if your popup is open or not.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("shouldShowPopup", isPopupOpen);
}
Then, on your onCreate
method, you should check if the Bundle param is null. If is not null you can get the value we have saved before and show (or not) the popup.
Upvotes: 1