Skip to content

Instantly share code, notes, and snippets.

@kubode
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubode/ad8af469b98686536ad1 to your computer and use it in GitHub Desktop.
Save kubode/ad8af469b98686536ad1 to your computer and use it in GitHub Desktop.
キーイベント関連
  • キーイベントは子から親へは伝えられない
  • カスタムViewからActivityのBackを呼びたい場合、View#dispatchKeyEvent(KeyEvent)じゃ無理
  • Activityの参照が取れればいいが、getContext()はActivityである保証はない
  • Instrumentation使う
private void sendBackKey() {
  new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
      new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
      return null;
    }
  }.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment