1

When you first launch the app opens LoginActivity, next times you launch it opens MainActivity. LoginActivity only need to remember the username, etc. Everything else is in MainActivity.

So LoginActivity launches too slow. Please help to optimize the application to launch it faster.

It's my LoginActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    notFirstInit();

    spinner = (AppCompatSpinner) findViewById(R.id.spinner);
    editText = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.login_button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            name = editText.getText().toString();
            group = spinner.getSelectedItem().toString();

            saveUserData();

            startMainActivity();
            finish();
        }
    });
}

private void saveUserData() {
    editor = settings.edit();
    editor.putString("name", name);
    editor.putString("group", group);
    editor.apply();
}

private void notFirstInit() {
    settings = getSharedPreferences("settings", Context.MODE_PRIVATE);

    if (settings.getBoolean("activity_executed", false)) {
        startMainActivity();
        finish();
    } else {
        editor = settings.edit();
        editor.putBoolean("activity_executed", true);
        editor.apply();
    }
}

private void startMainActivity() {
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

You can see how much time it takes to launch in logcat:

08-13 00:07:12.500 6996-6996/uz.triplet.tuitguide W/System: ClassLoader referenced unknown path: /data/app/uz.triplet.tuitguide-1/lib/arm
08-13 00:07:21.927 6996-6996/uz.triplet.tuitguide W/System: ClassLoader referenced unknown path: /data/app/uz.triplet.tuitguide-1/lib/arm
08-13 00:07:21.929 6996-6996/uz.triplet.tuitguide I/LoadedApk: No resource references to update in package common
08-13 00:07:21.930 6996-6996/uz.triplet.tuitguide I/LoadedApk: No resource references to update in package com.chummy.jezebel.material.dark.regression
08-13 00:07:22.295 6996-7370/uz.triplet.tuitguide D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: false

                                                                    [ 08-13 00:07:22.310  6996: 6996 D/         ]
                                                                    GPU monitor inactive - could not find libgpumonitor.so [(null)] or not enabled (debug.egl.hw.gpumonitor = 0)
08-13 00:07:22.359 6996-7370/uz.triplet.tuitguide D/libEGL: loaded /vendor/lib/egl/libGLES_vc4.so
08-13 00:07:22.407 6996-7370/uz.triplet.tuitguide I/OpenGLRenderer: Initialized EGL, version 1.4
08-13 00:07:22.410 6996-7370/uz.triplet.tuitguide W/egl_server: Allocating storage 1x1 surface type 0x1
08-13 00:07:22.421 6996-7370/uz.triplet.tuitguide W/khrn_client: init_window num_buffers 3 min undequeued buffers 1
08-13 00:07:22.422 6996-7370/uz.triplet.tuitguide W/khrn_client: init_window window 0x43747af8, 480x800 hintTransform 0x0 do_pre 1

UPD: I don't know why but when I'm building new APK using Build -> Build APK application opens instantly. What is the problem with running app clicking the button Run

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Khojiakbar
  • 559
  • 5
  • 14
  • Are you using emulator? Maybe the problem is not in your app but in the emulator. Try intel x86 instead of arm. If you run it on a smartphone, does it continue to be slow? – rafaelc Aug 12 '16 at 20:27
  • I'm using smartphone (Samsung Galaxy S2 Plus). MainActivity opens fast even second time I launch the app, when starts LoginActivity checks for preferences and lunches MainActivity. – Khojiakbar Aug 12 '16 at 20:33
  • Which gradle version you are using? – Pravin Divraniya Aug 13 '16 at 16:24
  • disable instance run may fix the issue! see my question here http://stackoverflow.com/questions/36623917/first-launch-take-long-time-in-android-studio-2-0-gradle-2-0 – Amir Aug 13 '16 at 16:46

1 Answers1

0

you can use a AsyncTask to launch heavy tasks of your MainActivity , like this :

public void StartProgress() {
    new AsyncProgressBar().execute();
}



private class AsyncProgressBar extends AsyncTask<Void, Void, Void> {




    protected ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("loading...");
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        //duration of progressbar
        SystemClock.sleep(5000);

        return null;
    }



    @Override
    protected void onPostExecute(Void useless) {
       //your task


        dialog.dismiss();
    }
}
Marl Ene
  • 24
  • 8
  • 1
    The problem is not in this. In the first Activity there is no such tasks, which requires 10 seconds to run. – Khojiakbar Aug 13 '16 at 16:17