Resume last activity when launcher icon is clicked in android

Resume last activity when launcher icon is clicked in android

I came across this interesting problem, where my app was starting fresh from root/launcher activity, instead of resuming from background. At first this issue was really hard to reproduce, as the app was working absolutely fine while debugging the app or running from eclipse. After some rigorous testing, we identified that the faulty behaviour is shown only when app is installed from a release build and the problem disappears when app is closed from background and launched again.

Now was the time to do the research and after failed initial attempts, finally I came to know that this is a known issue of android. When you click the application icon in the launcher a new Activity will be created on top of the existing one. The single task flag, if you are using one, seems to be ignored for some unknown reason. And as usual, StackOverflow came to my rescue, that provided this piece of code and my problem was resolved:

if (!isTaskRoot()) {
finish();
return;
}

Here are the references related to this bug: