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?