-2

I am working on a custom launcher where I am trying to override the Back button which is not working in the desired way.

The objective of this launcher is to launch a particular app (pre-defined) and load the list of installed Apps on back press. When this launcher is made to run it launches the desired App but loads the list of installed Apps on double back press. On single back press it either takes the user to a black screen or the previous screen (if any) and on another back press it loads a list of Apps. I have used keyCode and onBackPressed().

The code is below.

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Intent i = new Intent(this, AppListActivity.class);

        startActivity(i);

        this.finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
jwpfox
  • 5,124
  • 11
  • 45
  • 42
SCode
  • 1
  • 1

2 Answers2

0

Comment

return true;

Try it and let me know.. It works for me

Preetika Kaur
  • 1,991
  • 2
  • 16
  • 23
  • public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { Toast.makeText(this, "testing", Toast.LENGTH_SHORT).show(); Intent i = new Intent(this, sample2.class); startActivity(i); this.finish(); // return true; } //return false; return super.onKeyDown(keyCode, event); } Check this Its wrking for me and Its same as your one – Preetika Kaur Sep 15 '16 at 11:18
  • In my App the intent switch is not in same App it is changing between two Apps i.e not between activities but between Apps too . – SCode Sep 15 '16 at 11:28
0

This might be a duplicate of this

Since one backPress means has one action and double-tap has one, you will need to have some kind of counter, which also means a delay. I believe this is what you are looking for, which is inspired by the answer in the link above. There will be a 500ms delay on a single backPress. Hope it helps.

Rewritten to match your needs, but basically what is says:

boolean doubleBackToExitPressedOnce = false;
private static final int PERIOD = 500; 

@Override
public void onBackPressed(){
    if (doubleBackToExitPressedOnce) {
        Intent i = new Intent(this, LoginActivity.class);

        startActivity(i);

        this.finish();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
            back();
        }
    }, PERIOD);
}
public void back(){
    super.onBackPressed();
}

EDIT: I guess I misunderstood what you were trying to do. I believe this should work if you replace

Intent i = new Intent(this, LoginActivity.class);
startActivity(i);
this.finish();

with

Intent launchIntent =     getPackageManager().getLaunchIntentForPackage("com.package.address");
if (launchIntent != null) { 
    startActivity(launchIntent);
}

and use the correct package name. This answer was found here

Community
  • 1
  • 1
MrLys
  • 28
  • 4
  • @SCode How about now? – MrLys Sep 15 '16 at 11:57
  • In this module Scenario is, I am currently on different App where on backpress I need to enter again in my App i.e entering in activity of my app.On adding this code there is no operation on backpress, screen does not change. – SCode Sep 16 '16 at 06:40