Android Delays when launching Activity from another app

I have 2 applications. Apps A and App B only have BActivity (App B Package: com.ts.share). From Appendix A, I would like to start Appendix B. In Appendix A, I named

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

      

It worked fine.

In B, If exit is B, go to A and call again. It took 0 seconds to start the application.

But in B press the Home button, navigate to A and call again. It took 3 seconds to launch the application.

I want application B to start right away.

I appreciate your help!

+3


source to share


3 answers


try it.



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);

      

0


source


In your second scenario, B is already running when you start it from A. In this case, since you specified Intent.FLAG_ACTIVIY_CLEAR_TOP

, it will need to finish existing activities that are still active in B before it can instantiate a new activity in B. Perhaps that you have the code in the finish()

, onPause()

, onStop()

or onDestroy()

activity (or activities) in B, which causes a delay of 3 seconds.



0


source


It is possible that your main thread is doing a lot of work. An alternative solution would be to load the intent of a separate handler as well.

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 );
    }
});

      

0


source







All Articles