Intent is very slow to launch a new Activity :(
Asked Answered
E

1

9

I have this piece of code for an Intent:

Intent i = new Intent();
        i.setAction(Intent.ACTION_MAIN);
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        i.setComponent(new ComponentName(packToLaunch, nameToLaunch));
        startActivity(i);

This basically launches a new activity based on the package name that I pass to it. Sometimes, it takes up to 5 seconds to launch this new Activity. Is there any way to speed this process up? It even takes this long when I have an app that is still running. Please help...

Electrocorticogram answered 2/5, 2011 at 4:1 Comment(5)
What activity in what app are you trying to start? Activity startup time is influenced by how much work the activity is trying to do during its lifecycle startup calls (onCreate, etc.)Meltwater
If you are just testing in emulator, activity changes take much much longer than on an actual handset. Especially while in debug mode. But do as adamp said, check your called activities onCreate for inefficient code.Devondevona
I have a service that sends this intent and sometimes, it takes forever to start the app. I have noticed that if the app is running, it launches instantly so I guess I really have no control over how much processing the app needs in it's onCreate method. Is there maybe a way to start the apps intended for this intent when my user selects it in order for it to be launched into memory without actually showing it, kinda like prefetching?Electrocorticogram
I've the same problem with the ..NEW_TASK flag. Any ideas?Complicate
Does this answer your question? Starting an activity from a service after HOME button pressed without the 5 seconds delayDhammapada
W
7

It looks like Android intentionally delays launch of activity from service right after you press HOME button. (When using BACK button everything is OK.) There was even issue posted https://code.google.com/p/android/issues/detail?id=4536 however it got obsolete.

I tried to search actual implementation of the delay in Android source, but failed. You might want to check the following question as it states pretty same issue and gives some more insights: Starting an activity from a service after HOME button pressed without the 5 seconds delay

Whomever answered 18/11, 2016 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.