Android: OnKeyDown problem
I am using onKeyDown for handling the back button but on pressing the back button the application exits whereas it should go back to the previous activity. Following is the code i'm using:
public class NewsDetails extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent myIntent = getIntent();
String id = myIntent.getStringExtra("content_id");
String title = myIntent.getStringExtra("title");
String nTitle = "<font color='white'>"+title+"</font>";
setContentView(R.layout.web);
super.onStart();
final String mimeType = "text/html";
final String encoding = "utf-8";
/*Title*/
WebView wv = (WebView)findViewById(R.id.wv1);
WebSettings webSettings = wv.getSettings();
webSettings.setDefaultFont开发者_如何学GoSize(15);
wv.setBackgroundColor(Color.BLACK);
wv.loadData(nTitle, mimeType, encoding);
/*Body*/
String xml = XMLfunctions.getBodyXML(id);
String result = xml.replaceAll("<p>", "<p><div align=\"justify\">");
String nXml = result.replaceAll("</p>", "</div></p>");
String nBody = "<font color='white'>"+nXml+"</font>" ;
WebView wv1 = (WebView)findViewById(R.id.wv2);
wv1.setBackgroundColor(Color.BLACK);
WebSettings webSettings1 = wv1.getSettings();
webSettings1.setDefaultFontSize(10);
wv1.loadData(nBody, mimeType, encoding);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Intent intent = new Intent(NewsDetails.this, TopNewsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
View view = TopNewsGroup.group.getLocalActivityManager().startActivity("ShowNews", intent).getDecorView();
TopNewsGroup.group.setContentView(view);
return true;
}
return super.onKeyDown(keyCode, event);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionsmenu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.refresh:
startActivity(new Intent(this, NewsDetails.class));
return true;
case R.id.search:
startActivity(new Intent(this, SearchActivity.class));
return true;
case R.id.info:
startActivity(new Intent(this, NewsDetails.class));
return true;
case R.id.exit:
finish();
return true;
}
return false;
}
}
Use the following code in place of onKeyDown(..
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
{ // do something on back.
Intent lIntentObj = new Intent(NewsDetails.this, TopNewsActivity.classs);
startActivity(lIntentObj);
finish();
return true;
}
return super.dispatchKeyEvent(event);
}
As i'm using ActivityGroup i wasn't using the history stack but on using
public void back() {
if(history.size() > 0) {
history.remove(history.size()-1);
if(history.size()<=0){
finish();
}else{
setContentView(history.get(history.size()-1));
}
}else {
finish();
}
}
@Override
public void onBackPressed() {
TopNewsGroup.group.back();
}
i was able to move back to the previous activity.
This should work. If I remember correctly you should override onKeyDown and handle the back button in onKeyUp:
@Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
@Override
public boolean onKeyUp(final int keyCode, final KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Intent intent = new Intent(NewsDetails.this, TopNewsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
View view = TopNewsGroup.group.getLocalActivityManager().startActivity("ShowNews", intent).getDecorView();
TopNewsGroup.group.setContentView(view);
return true;
}
return false;
}
精彩评论