0

I'm in the process of creating a set of home/back keys for when the statusbar in android is hidden. I've designed a button that simulates a home-key press as such

private void InjectKey(final int keyEventCode) {
       new Thread(new Runnable() {
        @Override
        public void run() {
               new Instrumentation().sendKeyDownUpSync(keyEventCode);
        }
       }).start();

}

which works just fine for home-key presses. However, when I try to apply the same solution to the back button, the app obviously just calls finish() on itself.

My question is this: How do I implement the backbutton so that it calls "back" on the latest app and on itself?

Thanks!

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
MathiasStokholm
  • 121
  • 2
  • 3

1 Answers1

0

In second activity if you uses your own back button, and uses finish(); to stop that activity then following method will be called. so override folloing method in your Second Activity and put needed code in it

@Override
    public void onStop() {
        super.onStop();
        Toast.makeText(Activity2.this, "STOP", Toast.LENGTH_SHORT).show();
    }

In the Second Activity, you can use Override following method Which will be called when back button is called

@Override
    public void onBackPressed() {
            Toast.makeText(Activity2.this, "BACK", Toast.LENGTH_SHORT).show();
            super.onBackPressed(); // allows standard use of backbutton for page 1
    }

Put needed code in this method.

Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33