Android-如何覆盖“Back”按钮,这样它就不会完成我的活动?我目前有一个活动,当它被显示时,通知也会显示在Notificationbar中。这样,当用户按home并将活动推送到后台时,他们就可以通过通知返回到活动中。当用户按下“后退”按钮,我的活动就会被销毁,但通知仍然是我希望用户能够按下,但仍然能够通过通知到达活动时出现的问题。但是当用户尝试这一点时,我会得到Null指针,因为它试图启动一个新的活动,而不是带回旧的活动。因此,本质上,我希望后退按钮的动作与Home按钮完全一样,下面是我到目前为止所做的尝试: @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5
&& keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Log.d("CDA", "onKeyDown Called");
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
return;
}然而,上面的代码似乎仍然允许我的活动被销毁,当按下“后退”按钮时,我如何才能阻止我的活动被销毁呢?
3 回答
侃侃尔雅
TA贡献1801条经验 获得超16个赞
true
KEY_BACK
.
onBackPressed()
).
startForeground()
@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) { if (Integer.parseInt(android.os.Build.VERSION.SDK) > 5 && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { Log.d("CDA", "onKeyDown Called"); onBackPressed(); return true; } return super.onKeyDown(keyCode, event);}@Overridepublic void onBackPressed() { Log.d("CDA", "onBackPressed Called"); Intent setIntent = new Intent(Intent.ACTION_MAIN); setIntent.addCategory(Intent.CATEGORY_HOME); setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(setIntent);}
潇湘沐
TA贡献1816条经验 获得超6个赞
@OverrideonSaveInstanceState(Bundle frozenState) { frozenState.putSerializable("object_key", someSerializableClassYouWantToPersist); // etc. until you have everything important stored in the bundle}
@OverrideonCreate(Bundle savedInstanceState) { if(savedInstanceState!=null){ //It could be null if starting the app. mCustomObject = savedInstanceState.getSerializable("object_key"); } // etc. until you have reloaded everything you stored}
- 3 回答
- 0 关注
- 399 浏览
添加回答
举报
0/150
提交
取消