-2

I have seveal actvities in my app, but switching between them is too long. I switch between activities like this:

onClick(View v) {
    startActivity(new Intent(FirstLaunchActivity.this, MainActivity.class));
    finish();
}

And new Activity's onCreate:

super.onCreate(savedInstanceState);

        // Setting Content View
        setContentView(R.layout.activity_main);

        if (getIntent().getBooleanExtra(EXIT_KEY, false)) {
            finish();
        }

        if (savedInstanceState != null) {
            mSignInProgress = savedInstanceState
                    .getInt(SAVED_PROGRESS, STATE_DEFAULT);
        }

        mClient = buildGoogleApiClient();
        settings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);
        settingsVK = getSharedPreferences(VK_PREFERENCES, MainActivity.MODE_PRIVATE);

// Load Navigation Drawer
        getDrawer(
                settingsVK.getString(VK_INFO_KEY, ""),
                settingsVK.getString(VK_EMAIL_KEY, "")
        );

/** GOOGLE **/
        mSignInButoon = (SignInButton) findViewById(R.id.sign_in_button);
        mSignInButoon.setOnClickListener(this);

        mSignOutButton = (Button) findViewById(R.id.sign_out_button);
        mSignOutButton.setOnClickListener(this);
/****/

        if (getIntent().getBooleanExtra(JUST_SIGNED_KEY, false))
            initDialog_share();

        // Get and set system services & Buttons & SharedPreferences & Requests
        inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);

        vk_sign_out = (CustomView) findViewById(R.id.vk_sign_out);
        vk_sign_out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected void onPreExecute() {
                        startActivity(new Intent(MainActivity.this, FirstLaunchActivity.class)
                                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                        finish();
                    }

                    @Override
                    protected Void doInBackground(Void... params) {
                        settings
                                .edit()
                                .putBoolean(SIGNED_IN_KEY, false)
                                .putString(PROFILE_PHOTO_LOCATION_KEY, "")
                                .apply();
                        settingsVK
                                .edit()
                                .putString(VK_PHOTO_KEY, "")
                                .putString(VK_EMAIL_KEY, "")
                                .putString(VK_INFO_KEY, "")
                                .putBoolean(VK_SIGNED_KEY, false)
                                .apply();

                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        super.onPostExecute(aVoid);
                    }
                }.execute();
            }
        });

        if (!settingsVK.getBoolean(VK_SIGNED_KEY, false)) {
            vk_sign_out.setEnabled(false);
        }

I used AsyncTask and Theads, but still need wait too long. How can I optiimize my code?

Roman Svyatnenko
  • 699
  • 12
  • 27
  • It basically depends on what you want to do, and is based heavily on opinion. I barely use more than 1 activity in my apps, other people encourage using 3 - 5 activities and fragments, while other advocate managing everything in Views. So your answer really can't be answered clearly in my opinion – Benjamin Scharbau Dec 03 '15 at 01:23
  • Do not do too much stuff in onCreate(or other callbacks), use other thread for loading the resources... So in other words, do not block the main thread... – Selvin Dec 03 '15 at 01:23
  • @Selvin, it became slower, using new threads... – Roman Svyatnenko Dec 03 '15 at 02:17
  • Obviously, creating threads take some additional time... But shouldn't down drastically the speed... But what is more important, moving long running tasks out of main thread will make app more responsive... And obviously 3-5 sec. between activities switching means that you are doing to much work on main thread,, end of story. – Selvin Dec 03 '15 at 02:25
  • @Selvin, I put 80% of OnCreate content to new thread, but still have 3-5 sec. – Roman Svyatnenko Dec 03 '15 at 02:28
  • ... And then called run not start... or wrapped 100 % of the Thread's run method with Handler.post or runOnUiThread... Without code it is hard to say what is a real reason of this. – Selvin Dec 03 '15 at 02:31
  • https://drive.google.com/file/d/0B61_ocdRW25TdmlHYU51dGxrSnc/view?usp=docslist_api less then 1 sec... At 0:08 I clicked the button at 0:09 new activity is ready... See how many elements this ListView has... But still it's quick... – Selvin Dec 03 '15 at 02:50
  • @Selvin, thank you for your anwser. I am beginner. I will learn more about threads. – Roman Svyatnenko Dec 03 '15 at 03:05
  • click on below link and you will get your answer... http://stackoverflow.com/questions/20306091/dilemma-when-to-use-fragments-vs-activities – user5519264 Dec 03 '15 at 04:56
  • I'd use a single Activity and swap the Fragments inside it. – Phantômaxx Dec 03 '15 at 08:53

1 Answers1

0

No need to finish activity every time.

for example

Page 1,2,3,4 is an application to register user

When I am going to start page 2, I just startActivity, no need to call finish. If I call backbuttonPressed() function, every thing is ok. (super.backbuttonPressed() already contain finish()) I don't need to reset page 1 and use it back easy.

If I finished the page4 registration and want to go back main page, I will call startActivity(page1) and call finish page4 after that.