Search code examples
android-studioauthenticationandroid-activitysharedpreferencessplash-screen

Go to another activity not main activity after splash screen


I have a splash screen in which i am checking user is logged in or not if logged in go to dashboard otherwise go to login activity. I am using sharedpref. Issue is (which I am unable to resolve) after splash screen login screen appears for a brief moment than dashboard. Splash > Login > Dash what i Want is Splash > Dash (if user logged in). Login is the main activity of my project. Here is the code:

public class SplashScreen extends AppCompatActivity {

private SessionManager sessionManager;
private BroadcastReceiver broadcastReceiver;
private SharedPreferences prefs;
private boolean isLogin;
private int accessID;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sessionManager = new SessionManager(this);
    broadcastReceiver = new CheckNetStatus();
    broadcastIntent();
}

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onResume(){

    super.onResume();

    new CheckNetStatus().onReceive(SplashScreen.this,new 
 Intent(ConnectivityManager.CONNECTIVITY_ACTION));

    try {
        prefs = getSharedPreferences(SessionManager.PREF_NAME, 0); // Declare 
     SharedPreferences
        accessID = prefs.getInt(SessionManager.KEY_ACCESSID, 0); // get Access Id from 
   SharedPreferences
        isLogin = Utils.getLoginStatus(SplashScreen.this); // Check Login is true or false

    


    } catch (Exception e) {
        e.printStackTrace();
    }

    Thread splashTread = new Thread() {

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void run() { // run thread
            try {
                synchronized (this) {
                    Thread.sleep(3000); // Screen stay for 3 sec.
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (isLogin)
                {
                //    if (accessID == 0) { // access Id is ZERO open AddMoneyActivity.class
                //        try {
                //            } catch (Exception e) {
                //            e.printStackTrace();
                //        }
                //    } else if (accessID == 1) {// access Id is ONE open 
      ProfileStepOneActivity.class
                        try {
                            Intent intent = new Intent(SplashScreen.this, Dashboard.class);
                           
                            startActivity(intent);
                            finishAffinity(); // Finish stack
                        } catch (Exception e) {
                            e.printStackTrace();
                        }


            } else {// Login is False  goto Login  Activity
                try {
                    Intent intent = new Intent(SplashScreen.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
};
    splashTread.start();
  }

public void broadcastIntent() {
    registerReceiver(broadcastReceiver, new 
   IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
   }



@Override
protected void onPause() {
    super.onPause();
    try {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
        //unregisterReceiver(broadcastReceiver);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
protected void onStop(){
    super.onStop();
    try {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
 //            unregisterReceiver(broadcastReceiver);
    } catch (Exception e) {
        e.printStackTrace();
        }

   }
   }

Solution

  • My mistake....i was redirecting to main activity ie login activity in netstats class(where i was checking the net connection) ie why it was showing login screen for a moment issue resolved now