0

In my android application I have 3 activities A,B and C

Activity A is the launcher activity of my application, inside it there is a button with the following code when clicked:

startActivity(new Intent(this , B.class));
finish();

in activity B I have a button that starts activity C:

startActivity(new Intent(this , C.class));

In activity C, I need to finish the activity when the home button pressed:

public boolean onKeyDown(int keyCode,KeyEvent event)
{
    if (keyCode == KeyEvent.KEYCODE_HOME)
    {
        finish();
        return true;
    }
    return super.onKeyDown(keyCode,event);
}

Now I expect that the top activity in my task is activity B, but when I tap the app icon from launcher activity A is started, so it seems the whole task is ended somehow. Can someone explain what is going on and why am I getting this behavior?

rafaelasguerra
  • 2,685
  • 5
  • 24
  • 56
ammcom
  • 992
  • 1
  • 7
  • 24
  • Post your manifest entries for these activities. Do any of them contain [`android:clearTaskOnLaunch="true"`](https://developer.android.com/guide/topics/manifest/activity-element.html#clear) or [`android:launchMode="singleTask"`](https://developer.android.com/guide/topics/manifest/activity-element.html#lmode)? – Bryan Oct 10 '16 at 15:01
  • 2
    Also, the attribute [`android:noHistory="true"`](https://developer.android.com/guide/topics/manifest/activity-element.html#nohist) may work better for you, instead of listening for a *Home* button press. This attribute clears the `Activity` from the task after it is no longer visible on screen. – Bryan Oct 10 '16 at 15:09
  • No such attributes in the manifest – ammcom Oct 10 '16 at 15:10
  • When you say "when I tap the app icon from launcher", did you *close* the application beforehand? Or just put it in the background? – Bryan Oct 10 '16 at 15:13
  • Actually it is after I press the Home button as mentioned above – ammcom Oct 10 '16 at 15:17
  • `noHistory` attribute is not a good solution as activity C starts more activities using `startActivityForResult` and then if `noHistory` is true it will not go back again to activity C – ammcom Oct 10 '16 at 15:18

2 Answers2

1

Insert this code into your first activity and call it inside onCreate(...)

 private void killIfIsnotTaskRoot() {
    if (!isTaskRoot()
            && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
            && getIntent().getAction() != null
            && getIntent().getAction().equals(Intent.ACTION_MAIN)) {

        finish();
        return;
    }
}

... a new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created. In other circumstances — for example, if an existing instance of the "singleTop" activity is in the target task, but not at the top of the stack, or if it's at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.

take a look at this link How to prevent multiple instances of an activity when it is launched with different intents

Community
  • 1
  • 1
Jemo Mgebrishvili
  • 5,187
  • 7
  • 38
  • 63
  • OK this may solve the issue but I am asking why should activity A started? but when I press the home button when I am in activity B activity A is not started!! – ammcom Oct 10 '16 at 14:56
0

In the code you posted, activity B is trying to start activity B, NOT activity C.

in activity B I have a button that starts activity C:

startActivity(new Intent(this,B.class));

should be:

in activity B I have a button that starts activity C:

startActivity(new Intent(this,C.class));

greysqrl
  • 937
  • 3
  • 13
  • 31