Application or activity takes time to load some ti

2020-02-07 03:37发布

问题:

I have created a startup activity from where I am calling another activity which has a view pager and shows some introductory pages.

This app was taking time to load so I thought to display a progress dialog till the activity loads, but that progress dialog also appears few seconds later.

startup activity:

public class StartUpActivity extends AppCompatActivity {
    boolean isUserFirstTime, login;
    public static String PREF_USER_FIRST_TIME;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        isUserFirstTime = Boolean.valueOf(Utils.readSharedSetting(StartUpActivity.this, PREF_USER_FIRST_TIME, "true"));
        Intent introIntent = new Intent(StartUpActivity.this, SlidingActivity.class);
        introIntent.putExtra(PREF_USER_FIRST_TIME, isUserFirstTime);

        ProgressDialog dialog = new ProgressDialog(StartUpActivity.this);
        dialog.setMessage("Welcome to Mea Vita, please wait till the app loads.");
        dialog.setCancelable(false);
        dialog.setInverseBackgroundForced(false);
        dialog.show();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //Here you can send the extras.

                startActivity(new Intent(StartUpActivity.this,SlidingActivity.class));

                // close this activity
                finish();
            }
        }, 4000);
    }
}

This doesn't happen every time,only sometimes. What can be the possible reason for this? how can I stop this? Any solution? Thank you..

回答1:

There is a strange issue with newly released Android Studio 2.0 (same issue in 2.1) first time of launching application take longer than usual (e.g. 2, 3 seconds or sometimes screen blinks or goes black) this issue happens only in debug mode and not effect your released APK.

A temporary solution to fix this is disabling instant run:

Settings → Build, Execution, Deployment → Instant Run and uncheck Enable Instant Run


回答2:

First of all, make as rule to make all data loading in async tasks, you must check activity that you want to start where you load data.

The problem is in your second activity. oncreate method should be used only to make findviews or start async tasks, don't load any in oncreate or in onstart or in onresume. Probably you are loading high res images in sliding layout or you loading data in it.

There is another way, load all data in async task on first activity, then with ready data start second activity with already data loaded.



回答3:

There are a few things that can load slowly.

  1. Android need to read your code from storage and load the classes into ram.
  2. I assume Utils.readSharedSetting(StartUpActivity.this, PREF_USER_FIRST_TIME, "true") reads from preferences. That's a file that you're reading from synchronously.
  3. Actually launching the dialog takes a very small amount of time.

I'd suggest showing your loading inside the activity itself to minimize the work needed to render it.

Also, you can store PREF_USER_FIRST_TIME as a boolean instead of a String.