Anomaly with "Back" button when <min-sdk> used
I have encountered a very strange effect as I get near to finishing my project. I introduced the line
<uses-sdk android:minSdkVersion="8" />
into the manifest and re-ran the project in both the emulator and a phone (HTC Desire). I noticed that the "back" button wasn't operating in a number of areas of the program. When I put the code into debug, the onKeyDown
listener was firing OK [and handed off ok to super.onKeyDown(..)
] whereas the onBackPressed
listener wasn't firing at all.
When I removed the uses-sdk
entry from the manifest all returned to normal. Can anyone explain please because I really need to use the min-sdk
statement.
Update:
I have experimented right down to min-sdk="1开发者_开发知识库"
(the default value) and the effect is the same. I then removed the min-sdk value and used the target-sdk
value instead with EXACTLY THE SAME consequences i.e. the back button won't work in all places. Simply removing the tag <uses-sdk .... />
completely fixes the effect but leaves me with the problem of needing to declare a minimum sdk value before I deploy. Please help, someone, anybody...
Maybe we met the same problem. I use this way to simulate a soft back button, but when "min-sdk" is added, the button doesn't work. It seems like "dispatchKeyEvent" and "min-sdk" are in conflict. I used "onBackPressed" instead, it works fine for me.
public class BackButtonClickListener implements View.OnClickListener {
public void onClick(View v) {
Activity host = (Activity) v.getRootView().getContext();
host.onBackPressed();
}
}
Button back = (Button) findViewById(R.id.your_button_id);
back.setOnClickListener(new BackButtonClickListener());
Why not use onKeyDown?
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// do some stuff
return true;
}
return super.onKeyDown(keyCode, event);
}
精彩评论