4

I have 2 apps. App A and App B has only BActivity ( App B's Package is : com.ts.share). From App A , i'd like to start App B. In App A, i called

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.ts.share");
            LaunchIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity( LaunchIntent );

It worked fine.

At B, If exit B, go to A, and call again. It took 0s to start App B.

But At B, press Home button, go to A, and call again. It took 3 s to start App B.

I want to App B start immediately.

I appreciate your help!

Son Nguyen Thanh
  • 1,199
  • 15
  • 19

3 Answers3

0

In your second scenario, B is already running when you start it from A. In this case, because you've specified Intent.FLAG_ACTIVIY_CLEAR_TOP, it will need to finish the existing Activities that are still active in B before it can instantiate a new Activity in B. It is possible that you have code in finish(), onPause(), onStop(), or onDestroy() of the Activity (or Activities) in B that is causing the 3 seccond delay.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
-1

try this..

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
    Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.ts.share");
    LaunchIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity( LaunchIntent );
    }
}, 0);
M S Gadag
  • 2,057
  • 1
  • 12
  • 15
-1

It may be possible that your main thread is doing much work. An alternative solution would also be to load the intent on a separate handler.

Handler handler = new Handler(Looper.getMainLooper());

handler.post(new Runnable() {
    @Override
    public void run() {
        Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.ts.share");
            LaunchIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity( LaunchIntent );
    }
});
mangu23
  • 884
  • 9
  • 13