Android:OnKeyDown问题

时间:2011-05-03 07:40:25

标签: android

我正在使用onKeyDown来处理后退按钮,但是在按下后退按钮时应用程序退出,而它应该返回到之前的活动。以下是我正在使用的代码:

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.setDefaultFontSize(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;
}



}

3 个答案:

答案 0 :(得分:2)

使用以下代码代替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);
    }

答案 1 :(得分:1)

因为我正在使用ActivityGroup,所以我没有使用历史堆栈,而是使用

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();
}

我能够回到之前的活动。

答案 2 :(得分:0)

这应该有效。如果我没记错的话你应该覆盖onKeyDown并处理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;
}