The application authorizes the user (at the first login) via google, using fireBase.

The first launch of the application, authorize the user. On subsequent logins, redirect the user to the main Activity (if it is authorized) without showing the authorized activity.

SharedPreferences is not suitable.

Please do not throw stones at colleagues. I'm just learning android and I really need the advice of more experienced comrades. thank

public static final int SIGN_IN_CODE = 777; private GoogleApiClient googleApiClient; private ActivityMainBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .build(); googleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this, this) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build(); binding.buttonGoogle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient); startActivityForResult(intent, SIGN_IN_CODE); } }); } @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == SIGN_IN_CODE) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); handleSignInResult(result); } } private void handleSignInResult(GoogleSignInResult result) { if (result.isSuccess()) { goMapScreen(); } else { Toast.makeText(this, R.string.not_log_in, Toast.LENGTH_SHORT).show(); } } private void goMapScreen() { Intent intent = new Intent(this, ScreenMapActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } } 

    3 answers 3

    I solve a similar problem like this:

    1. There are 2 MainActivity , the MainActivity , which is declared to be the launcher in the manifest and the LoginActivity , in which the input manipulations are performed
    2. After authorization, LoginActivity writes an authorization token somewhere (depending on what we mean by authorization either in preferences or in the database or in FireBase or even in some cases just as a variable in MainActivity
    3. When launching the application in MainActivity.onCreate() check the fact of authorization (see above) if there is no authorization, we start something like startActivity(new Intent(context, LoginActivity.class))
    4. After successful authorization, close the LoginActivity.finish() and run the MainActivity from the LoginActivity through something like: startActivity(new Intent(context, MainActivity.class))

    Everything.

    • you can just bring the dialogue: "Hello !! Let's log in", login and password fields and the button "write me down" :) - pavlofff
    • Well, there is a lot of trouble with the dialogue, I have already tried - in order for the dialogue to work, you need to activate it, and the caller needs to be initialized somehow without authorization, you need to hide it or make the dialogue full screen - in general, the Activity is somehow more convenient with it. - Barmaley
    • @Barmaley If possible, please describe the implementation details. I did as you said. But it does not work for me. Anyway, the first is the activation of authorization. Implementation piece in MainActivity (Authorization) 'irebaseAuth = FirebaseAuth.getInstance (); if (firebaseAuth.getCurrentUser ()! = null) {finish (); } startActivity (new Intent (this, ScreenMapActivity.class)); ' - Dmitrii Tretiakov
    • @Barmaley I also do a check in the class that I need to open first for an authorized user ScreenMapActivity 'firebaseAuth = FirebaseAuth.getInstance (); if (firebaseAuth.getCurrentUser () == null) {startActivity (new Intent (this, MainActivity.class)); } ' - Dmitrii Tretiakov

    You need to create a new class for example SplashScreenActivity.class , which simultaneously performs two functions, shows a splash screen and simultaneously checks the authorization. To do this, you need to rewrite AndroidManifest.xml

    For example:

     <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar" /> <activity android:name=".SplashScreenActivity" android:screenOrientation="portrait" android:theme="@style/Theme.Design.Light.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

    SplashScreenActivity.class

     if (/* условие */) { Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class); startActivity(intent); } else { Intent intent = new Intent(SplashScreenActivity.this, SignInActivity.class); startActivity(intent); } 

    Edited here, so there may be errors

    If SharedPreferences is not suitable, you can store them in the internal folder of the application. File dir = new File (Environment.getCacheDirectory ());

    File stotage = new File (dir, "KEY_STORAGE");

    Write in the comments if it is not clear how to write and receive data from the file.

    But right away, it is more convenient for me to do this from: BufferedReader