Search code examples
androidandroid-permissionssplash-screen

Pause splash screen until user responds to permission request in android app


in my android application i have a splash screen that lasts 5 seconds during those 5 seconds i ask the user to grant location permission , I want to pause the splash screen activity until the user grants or denies the permission request.If the activity is not paused until the user responds, the next activity will be activated without having the user's response about the permission request here's my code

SpalshScreen activity:

public class SplashScreen extends BaseActivity {

    private static int SPLASH_SCREEN_TIME_OUT = 6000;

    some properties ....


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//will hide the title
        getSupportActionBar().hide(); //hide the title bar
        setContentView(R.layout.activity_splash_screen);

       // This is the method that asks the user for permission
        locationPermGranted = CheckLocationPermission();


        deviceLanguage = Locale.getDefault().getDisplayLanguage();
        DeviceLanguageCode = Locale.getDefault().getLanguage();
        sharedPreferences = getSharedPreferences(getPackageName() + ".prefs", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        codeIntent = new Intent(SplashScreen.this, PhoneVerification.class);
        myIntent = new Intent(SplashScreen.this, ProfileSettings.class);
        loggedIntent = new Intent(SplashScreen.this, Main.class);
        notLoggedIntent = new Intent(SplashScreen.this, Login.class);


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                if (!locationPermGranted) {

                    killApp();
                }

                if (sharedPreferences.getString("userLanguage", DeviceLanguageCode) == null) {
                    editor.apply();
                }

                if (sharedPreferences.getBoolean("isRegistered", false)) {


                    if (sharedPreferences.getBoolean("isPhoneVerified", false)) {


                        if (sharedPreferences.getBoolean("isLoggedIn", false)) {
                            // GO TO HOME

                            startActivity(loggedIntent);
                        } else {
                            // GO TO LOGIN
                            editor.apply();
                            startActivity(notLoggedIntent);
                        }

                    } else {
                        editor.apply();
                        startActivity(codeIntent);
                    }
                } else {
                    //GO TO PROFILE SETTINGS
                    editor.apply();
                    startActivity(myIntent);
                }

                finish();

            }
        }, SPLASH_SCREEN_TIME_OUT);

    } 

Solution

  • Check the permission up front and if the permission is already granted, continue your Handler() and if not, then request for the permission and handle the result. Here's how:

    1. Put all your Handler() code in a function so you can call it easily:

      private void continueOperation(){
          new Handler().postDelayed(new Runnable() {
              @Override
              public void run() {
      
                  if (sharedPreferences.getString("userLanguage", DeviceLanguageCode) == null) {
                      editor.apply();
                  }
      
                  if (sharedPreferences.getBoolean("isRegistered", false)) {
      
      
                      if (sharedPreferences.getBoolean("isPhoneVerified", false)) {
      
      
                          if (sharedPreferences.getBoolean("isLoggedIn", false)) {
                              // GO TO HOME
      
                              startActivity(loggedIntent);
                          } else {
                              // GO TO LOGIN
                              editor.apply();
                              startActivity(notLoggedIntent);
                          }
      
                      } else {
                          editor.apply();
                          startActivity(codeIntent);
                      }
                  } else {
                      //GO TO PROFILE SETTINGS
                      editor.apply();
                      startActivity(myIntent);
                  }
      
                  finish();
      
              }
          }, SPLASH_SCREEN_TIME_OUT);
      }
      

      Here, you don't have to check for permission because this function will only be called with granted permission.

    2. Then, put a permission check in onCreate():

      if (!locationPermGranted)
          killApp();
      else
          continueOperation();
      
    3. Now, handle the onRequestPermissionResult() and call continueOperation() if permission is granted.

      In case, you don't want the user to wait for 6 seconds if the permission is granted in onRequestPermissionResult() which would obviously be bad UX, you can use the Handler() in the else{} of 2nd point. So, User will only have to wait if the permission was previously granted which is the basic purpose of a SplashScreen.