Sunday 10 November 2013

Android Home key press behavior | Activity stack confused when launching app

Hello Droid Guys
I found an strange issue with the behavior of Home key press in android.Please
check below link if you want more details about this issue.


Actually, I think this is an android bug. If an app is installed via market, market 
update, or download from browser and the user launches the app directly from the
installer (ie: when the installer completes it offers the user the options to launch(open)
and done and when use select open the app now) it is launched in such a way that 
the OS gets confused.

If, after the app is launched, the user presses the HOME key to return to the home 
screen and then tries to return to the app by selecting it from the list of applications 
(or by putting a shortcut on the home screen and then selecting the shortcut), the OS
launches the root activity of the app AGAIN, bringing the existing task to the foreground
and placing ANOTHER instance of the root activity on top of the existing activity stack.

This behavior is extremely difficult to debug and has certainly caused countless hours 
of head-scratching by developers.

Note: I too found the same issue when I install an app from my mail and directly launch
the app.Go to Activity "A" then goes to Activity "B" and then I pressed "Home button".
Then ,When I open the same app pressing the app launcher on home screen,Instead of 
taking me directly to Activity "B" where I left it last time , It loads the app from the start.



It takes lots of my time to fixed this issue but finally I did that. I found the
way to resolve this problme :) .
I Added following code in the onCreate() method of my launcher activity and then Its
works fine for me.


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);

if (!isTaskRoot()) {
           Intent intent = getIntent();
           String action = intent.getAction();
           if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action != null &&                           action.equals(Intent.ACTION_MAIN)) {
               finish();
               return;
           }
    }
}

Hope, my above code will save some one time.
Enjoy Coding :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

1 comments:

  1. I too have the same prb this code helps me to overcome the issue...excellent thanks yaar..

    ReplyDelete

 

Copyright @ 2013 Android Developers Blog.