1

I am implementing a custom launcher. I have two activity : Activity A with launchMode : singleInstance or singleTask, and Activity B.

Activity A is main screen. There are 2 case :

  • If i set Activity A launchMode : singleTask, I call Activity B from Activity A (That's fine). In Activity B, if i press Home key to back main screen, then screen is empty(no any thing...).
  • If i set Activity A launchMode : singleInstance, I call Activity B from Activity A, then it shows a blank screen, after go to Activity B(in this case , I press Home key to bac main screen, it works fine).

So, how to resolve this? I want : Call Activity B from Activity A, then it go to Activity B directly, and when I press Home key, it go back to main screen (Activity A).

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

1

in Activity B you can use following code with back button

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Intent a = new Intent(this,A.class);
        a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(a);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}     

if you wants to work with home key than override Home key with following code

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
       if(keyCode == KeyEvent.KEYCODE_HOME)
        {
          //yours code action
return true;
        }
       if(keyCode==KeyEvent.KEYCODE_BACK)
       {
           //yours code action
return true;
       }
      return super.onKeyDown(keyCode, event);
    }

please you may try with this as well

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if ( (event.getKeyCode() == KeyEvent.KEYCODE_HOME) && isLock) {
            //yours code
            return true;
        }
        else
            return super.dispatchKeyEvent(event);
    }
Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
  • Thanks for your reply. In this case, you set SingleTask or SingleInstance ? Morever, I only get problem with Home key. I added onKeyDown in Activity B with keyCode = KeyEvent.KEYCODE_HOME, but this problem is not still working. – user1573559 Jan 13 '14 at 03:57
  • please write code like this...@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_HOME) { //yours code return true; } return super.onKeyDown(keyCode, event); } – Jitesh Upadhyay Jan 13 '14 at 04:05
  • I updated your code, but it doesn't run into OnKeyDown with KEYCODE_HOME. I don't understand. if i switch to case 2 (singleInstance). How to fix it? – user1573559 Jan 13 '14 at 04:16
  • did you add line in yours manifest – Jitesh Upadhyay Jan 13 '14 at 04:20
  • i updated the answer please use @Override public boolean dispatchKeyEvent(KeyEvent event) { if ( (event.getKeyCode() == KeyEvent.KEYCODE_HOME) && isLock) { //yours code return true; } else return super.dispatchKeyEvent(event); } – Jitesh Upadhyay Jan 13 '14 at 04:41
0

try this one... remove launchmode attribute from manifest and then,call Activity B from Activity A using finish() method as follows:

finish();
            Intent intent=new Intent(getApplicationContext(),ActivityB.class);
            startActivity(intent);
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40
  • As I ask above. There are 2 case : If i set Activity A launchMode : singleTask/singleTast/standard, I call Activity B from Activity A (That's fine). In Activity B, if i press Home key to back main screen, then screen is empty(no any thing...). How to fix it? If i set Activity A launchMode : singleInstance, I call Activity B from Activity A, then it shows a blank screen (How to fix it), after go to Activity B(in this case , i press Home key to back main screen, it works fine). – user1573559 Jan 14 '14 at 03:18