Intent sometimes not working without any error

561 views Asked by At

please help anyone, i have logout method using sweetalertdialog lib and this logout called in navigation drawer

private void logout() {

    SweetAlertDialog nDialog = new SweetAlertDialog(Master_Menu.this, SweetAlertDialog.CUSTOM_IMAGE_TYPE);

    nDialog.setTitleText("Alert");
    nDialog.setContentText("Are you sure you want to logout?");
    nDialog.setConfirmText("Yes");
    nDialog.setCancelText("No");
    nDialog.setCustomImage(R.drawable.ic_launcher);

    nDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
        @Override
        public void onClick(SweetAlertDialog nDialog) {
            //Getting out sharedpreferences
            SharedPreferences preferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
            //Getting editor
            SharedPreferences.Editor editor = preferences.edit();
            String level = preferences.getString(Config.LEVEL_USER,"null");

            //Puting the value false for loggedin
            editor.putBoolean(Config.LOGGEDIN_SHARED_PREF, false);

            //Stop the Service
            if (level.equalsIgnoreCase("KRN")) {
                stopService(new Intent(getApplicationContext(), getLocation.class));
                Log.d("Service", "STOP");
            }

            //Putting blank value to email
            editor.putString(Config.EMAIL_SHARED_PREF, "");
            editor.putString(Config.ID_SHARED_PREF, "");
            editor.putString(Config.TOKEN_SHARED_PREF, "");
            editor.putString(Config.LEVEL_USER,"");

            //Saving the sharedpreferences
            editor.commit();

            nDialog.dismiss();

            //Starting login activity
            Intent intent = new Intent(Master_Menu.this, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });

    nDialog.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
        @Override
        public void onClick(SweetAlertDialog nDialog) {
            nDialog.cancel();
        }
    });
    nDialog.show();

}

sometimes intent to loginActivity doesn't work, but theres no problem with confirm click listener because all shared preference has been change after i click confirm ( meaning the confirm function is working )

this is what i already try :

  1. i think have 2 intent in 1 method maybe become problem (stopservice and startactivity) but after i comment the stop service the problem still happen.
  2. i dont know differences about getapplicationcontext() and class.this in Intent so i just try to change from class.this to getapplicationcontext() because most of my intent in this project using getapplicationcontext, but the problem still happening too.

extra info :

size of my project ( after clean project ) around 400MB

and this is my lib

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/PhotoUtil.jar')
compile files('libs/GenAsync.1.2.jar')
compile files('libs/KGJsonConverter.jar')
compile 'com.android.support:multidex:1.0.0'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.volley:volley:1.0.0'
compile 'com.wdullaer:materialdatetimepicker:2.3.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.github.amigold.fundapter:library:1.0'
compile 'com.weiwangcn.betterspinner:library-material:1.1.0'
compile 'com.android.support:support-v4:24.2.1'
compile 'me.spark:submitbutton:1.0.1'
compile 'com.jaredrummler:material-spinner:1.1.0'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'cn.pedant.sweetalert:library:1.3'
compile 'com.github.PhilJay:MPAndroidChart:v2.1.6'
compile 'com.github.paolorotolo:appintro:4.0.0'
compile 'me.wangyuwei:ParticleView:1.0.4'
compile 'com.google.android.gms:play-services:8.4.0'

}

2

There are 2 answers

1
CodyEngel On

Try moving nDialog.dismiss(); to the last line of your setConfirmClickListener. You could be creating a race condition where the dialog may dismiss and be removed before it can start the new Activity.

0
Hassan Kalhoro On

Instead of using this you could use getApplicationContext(), it gets you the context of the application object for the currents process.

 Intent i = new Intent(getApplicationContext(), PreferencesActivity.class);
    startActivity(i);