Start a new Android activity so slow

I want to open a new action:

Intent intent = new Intent(homeScreen.this, EmployeeService.class);         
Bundle b = new Bundle();
b.putInt(Constants.SERVICE_DETAIL_L1_ID_MSG, ServiceIndex.SRV_L1_EMPLOYMENT);
b.putInt(Constants.SERVICE_DETAIL_FOCUS_POS_MSG, 2);
intent.putExtras(b);
startActivity(intent);

But it takes so long to make an Activity Activity (EmployeeService) visible. From Logcat, I see:

05-14 23:43:31.727: INFO/ActivityManager(59): Displayed activity fr.playsoft.happylille/.employee.EmployeeService: 7050 ms (total 7050 ms)

I can’t believe it takes more than 7 seconds to open a new activity. I am adding the onCreate () log, but I see that it only takes 5 ms to finish onCreate.

Can someone tell me how to find the root of this problem?

+5
source share
3 answers

You must move the code Html.fromHtml(desc);to the stream so that it is asynchronous. This thread can be safely started while onCreate()just opened Activity.

tvDesc.setText() :

class ExampleThread extends Thread {
    @Override
    public void run() {
         final Spanned spannedText = Html.fromHtml(desc);

         yourNewlyOpenedActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvDesc.setText(spannedText);
                }
         });
    }
}

, 7 , , 20 , ANR!

( Boy , /)

+3

Shlublu , , UI!

, , , .

, . 100 . 1 :

      new Thread() {

        @Override
        public void run() {
            while (true) {
                someTextView.setText("bla");

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();

: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

, Html.fromHtml(desc) AsyncTask RxJava

+1

your open new action code is placed in Thread. and running the code may be less than the time it takes to open another action. Maybe useful.

0
source

All Articles