Monday, September 16, 2013

Issue with resuming application after Home button is pressed on Android

Standard
Hello:

In this post, I am going to show how to fix the issue which comes while resuming application on Android. An application is moved to background when another application is launched or HOME button is pressed on Android devices. Sometimes the application which is moved to background is not resumed (where it was interrupted), resulting in relaunch of the application.



This issue has been asked multiple times on StackOverflow

  1. here
  2. here
This has also been reported multiple times on code.google.com/p/android.
One of the possible solutions for this issue (as of now)  is to have following code in OnCreate() of the launch activity of the application.

if (!isTaskRoot()) {
    final Intent intent = getIntent();
    final String intentAction = intent.getAction();
    if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) &&
            intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
        finish();
    }
}
Let me know if this was helpful.
Thanks for printing this post. Hope you liked it.
Keep visiting and sharing.
Thanks,
Ashwani.

0 comments :

Post a Comment