1

My application content part had too much code. There were about 3000 lines of XML code. This caused my application to startup slowly. (launch in about 8 seconds) I placed the content in 6 viewstub objects. and I created a lot of handlers. Is it a problem? Is it hierarchically correct? How can I do all these handler operations asynctask.

Also how can I make my content lighter and faster.

Thanks in advance!

 new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            viewStubPager.setLayoutResource(R.layout.viewstubpager);
            coachStubPager = viewStubPager.inflate();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    viewStub1.setLayoutResource(R.layout.viewstub1);
                    coachStub1 = viewStub1.inflate();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            viewStub2.setLayoutResource(R.layout.viewstub2);
                            coachStub2 = viewStub2.inflate();
                            viewStub3.setLayoutResource(R.layout.viewstub3);
                            coachStub3 = viewStub3.inflate();
                            viewStub4.setLayoutResource(R.layout.viewstub4);
                            coachStub4 = viewStub4.inflate();
                            viewStub5.setLayoutResource(R.layout.viewstub5);
                            coachStub5 = viewStub5.inflate();
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                                    Objects.requireNonNull(notificationManager).cancelAll();
                                    sharedPreferencesKeys();
                                    initialize();
                                    calculate();
                                    sharedPrefStartup();
                                    alertDialogClickListener();
                                    changeListener();
                                    new Handler().postDelayed(new Runnable() {
                                        @Override
                                        public void run() {
                                            layouts = new int[]{R.layout.vki_slide1, R.layout.vki_slide2, R.layout.vki_slide3, R.layout.vki_slide4, R.layout.vki_slide5, R.layout.vki_slide6, R.layout.vki_slide7, R.layout.vki_slide8, R.layout.vki_slide9};
                                            VKIPagerAdapter = new MyViewPagerAdapter();
                                            vkipager.setAdapter(VKIPagerAdapter);
                                            VKIPagerAdapter.notifyDataSetChanged();
                                            vkipager.setOffscreenPageLimit(10);
                                            vkipager.addOnPageChangeListener(viewPagerPageChangeListener);
                                            pageIndicator.setCount(layouts.length);
                                            pageIndicator.setSelection(0);
                                            bottombar.setVisibility(View.VISIBLE);
                                        }
                                    }, 100);
                                }
                            }, 100);
                        }
                    }, 100);
                }
            }, 100);
        }
    }, 150);
theoyuncu8
  • 73
  • 8
  • This is really difficult to read. Are the delay values (100, 150ms) passed to `Handler#postDelayed` necessary? `Handler#post` works similarly but without an explicit delay. It may also help to create named instances of `Runnable` with well-defined parameters. – Tom Jan 27 '20 at 20:09
  • I put all the content part in 6 viewstubs. Then I'm creating a handler launcher and set / inflate the viewstubs. When one handler is over (100 ms), I'm starting the other handler. Would this be a problem? @Tom – theoyuncu8 Jan 28 '20 at 08:31
  • Explicit delays are problematic because they introduce race conditions. Performance is often inconsistent across different hardware and software, so accurate timing is difficult. It's best to perform critical UI work in a blocking way. – Tom Jan 28 '20 at 14:25
  • Can you give an example @Tom – theoyuncu8 Jan 28 '20 at 16:32
  • Google has some extensive documentation of instrumentation and performant UIs (https://developer.android.com/topic/performance/vitals/render). From this code snippet alone, it's not clear exactly what Runnable/ code block is taking the bulk of the time so it's difficult to suggest what to optimize. – Tom Jan 28 '20 at 18:45

1 Answers1

0

It's a bit late, but this might help others...

I don't know if the code in the question will start faster with my proposed solution (I doubt this will be the case), however, it's way more readable and uses only one Runnable. This can also be used for animations.

I derived this example from the following answer: https://stackoverflow.com/a/11198037/6423246

Handler mHandler = new Handler();
int inflater = YOUR_CONSTANT_1;

void yourFunction() {

    // ...your first inflater code here...

    mHandler.postDelayed(mRunnable, your_delay_in_millis);
}

Runnable mRunnable = new Runnable() {
    @Override
    public void run() {
        switch(inflater) {
            case YOUR_CONSTANT_1 : {

                // ...your second inflater code here...

                inflater = YOUR_CONSTANT_2;
                mHandler.postDelayed(mRunnable, your_delay_in_millis);
                break;
            }
            case YOUR_CONSTANT_2 : {

                // ...your third inflater code here...

                inflater = YOUR_CONSTANT_3;
                mHandler.postDelayed(mRunnable, your_delay_in_millis);
                break;
            }

            // etcetera

            case YOUR_CONSTANT_LAST : {

                // ...your last inflater code here...

                // in your final case, you could opt to remove callbacks
                mHandler.removeCallbacks(mRunnable);
                break;
            }
        }
    }
};