Android后退按钮不起作用

时间:2013-11-02 06:40:09

标签: android

public class Myactivity extends DroidGap {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myactivity);
    }

    @Override
    public void onDestroy() {
        this.finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.myactivity, menu);
        return true;
    }

    @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
          super.loadUrl("file:///asset/www/index.html");
        }
        return super.onKeyDown(keyCode, event);
    }

在上面我需要在单击后退按钮时转到index.html文件。但是当我尝试它时,我会得到以下错误。

11-02 12:25:00.677: E/WindowManager(24344): Activity org.apache.cordova.example.Myactivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@421fad78 that was originally added here
11-02 12:25:00.677: E/WindowManager(24344): android.view.WindowLeaked: Activity org.apache.cordova.example.Myactivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@421fad78 that was originally added here

2 个答案:

答案 0 :(得分:0)

后退按钮应该完成活动。调用super.onKeyDown(keyCode, event)你要求android调用currentActivity.finish();

答案 1 :(得分:0)

覆盖onBackPressed()方法。把你的代码放进去。它会起作用