我有一个实现的活动,View.OnKeyListener
以便它可以捕捉用户点击后退按钮并弹出一个对话框。以前,它工作得很好。然而,最近它已经停止工作,并且该应用程序只会假装没有onKey
方法可以捕捉“后退”按钮。我似乎无法弄清楚为什么,因为onKey
自从我做出改变以来我几乎没有接触过这个方法。我尝试只使用OnBackPressed()
,但这并没有解决任何问题,实际上产生了奇怪和不可预测的行为。谁能帮我弄清楚为什么我的应用程序似乎让系统在后退按钮上占据主导地位?
这是我实现的重要部分:
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuInflater;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
public class NagTasksAddTasksActivity extends SherlockFragmentActivity implements View.OnKeyListener {
//various parameters
public void onCreate(Bundle savedInstanceBundle)
{
super.onCreate(savedInstanceBundle);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String setTheme = prefs.getString("themeselect", "Dark Overflow");
if (setTheme.toLowerCase().contentEquals("dark overflow")){
setTheme(R.style.Theme_Sherlock_ForceOverflow);
} else if (setTheme.toLowerCase().contentEquals("light overflow")){
setTheme(R.style.Theme_Sherlock_Light_ForceOverflow);
isDarkThemeUsed = false;
} else if (setTheme.toLowerCase().contentEquals("dark menu")){
setTheme(R.style.Theme_Sherlock);
} else if (setTheme.toLowerCase().contentEquals("light menu")){
setTheme(R.style.Theme_Sherlock_Light);
isDarkThemeUsed = false;
}
setContentView(R.layout.createtasklayout);
ActionBar actionBar = getSupportActionBar();
setTitle(R.string.newTask);
}
...
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(R.string.wait);
builder.setMessage(R.string.confirmbackkey);
builder.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//stuff
dialog.dismiss();
}
});
builder.setNegativeButton(R.string.discard, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog.Builder subBuilder = new AlertDialog.Builder(getParent());
subBuilder.setCancelable(true);
subBuilder.setTitle(R.string.wait);
subBuilder.setMessage(R.string.confirmdiscard);
subBuilder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//more stuff
dialog.dismiss();
}
});
subBuilder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.dismiss();
subBuilder.create().show();
}
});
builder.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.create().show();
return true;
}
return false;
}
}