在我的应用程序中,我编写了 onkeyDown 方法来处理我的菜单和后退按钮,当编写它的活动正常工作时,它工作正常,但是当我移动任何其他活动然后再次返回到此活动时,此方法不会作品。
请让我知道我的错误是什么或可能的解决方案是什么?
@SuppressWarnings("unchecked")
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(mExpandableListView.isGroupExpanded(commonGroupPosition)&& keyCode == KeyEvent.KEYCODE_MENU){
if(mllpopmenu.getVisibility() == View.VISIBLE){
mllpopmenu.setVisibility(View.INVISIBLE);
}
//Toast.makeText(getApplicationContext(), "Chat Expanded Menu", Toast.LENGTH_SHORT).show();
else{
if(commonGroupPosition>=4){
mllpopmenu.setVisibility(View.VISIBLE);
mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
R.layout.listviewtext, R.id.tvMenuText,
data_Contact));
}
else if(commonGroupPosition==1){
mllpopmenu.setVisibility(View.VISIBLE);
mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
R.layout.listviewtext, R.id.tvMenuText,
data_child_contact));
}
}
return true;
}
else{
if (keyCode == KeyEvent.KEYCODE_MENU) {
if (mllpopmenu.getVisibility() == View.VISIBLE) {
mllpopmenu.setVisibility(View.INVISIBLE);
}
else if(commonGroupPosition==0){
mllpopmenu.setVisibility(View.VISIBLE);
mlist2.setAdapter(new ArrayAdapter(UserMenuActivity.this,
R.layout.listviewtext, R.id.tvMenuText, data));
}
return true;
}
}
if(keyCode==KeyEvent.KEYCODE_BACK){
//Toast.makeText(UserMenuActivity.this, "Returning..", Toast.LENGTH_SHORT).show();
if (mllpopmenu.getVisibility() == View.VISIBLE) {
mllpopmenu.setVisibility(View.INVISIBLE);
} else {
if (CCMStaticVariable.CommonConnection.isConnected()) {
//super.onBackPressed();
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("CCM Logout");
alertDialog.setMessage("Are you sure you want to logout?");
alertDialog.setIcon(R.drawable.ic_launcher);
//Ok button for Logout
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
CCMStaticVariable.CommonConnection.disconnect();
Intent serviceIntent = new Intent(UserMenuActivity.this,UpdaterService.class);
stopService(serviceIntent);
//CCMStaticVariable.CommonConnection.removeConnectionListener(arg0);
finish();
}
});
//Cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
alertDialog.show();
}
}
return true;
}
return super.onKeyDown(keyCode, event);
}
谢谢