2017-08-21 49 views
3
public class MainActivity extends AppCompatActivity { 

    private LoginButton facebookloginButton; 
    private CallbackManager callbackManager; 
    private FirebaseAuth mAuth; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mAuth = FirebaseAuth.getInstance(); 

     facebookloginButton=(LoginButton)findViewById(R.id.facebook_login_button); 
     callbackManager=CallbackManager.Factory.create(); 
     facebookloginButton.setReadPermissions("email", "public_profile"); 
     facebookloginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       handleFacebookAccessToken(loginResult.getAccessToken()); 
      } 

      @Override 
      public void onCancel() { 

      } 

      @Override 
      public void onError(FacebookException error) { 
       Toast.makeText(getApplicationContext(), "Error",Toast.LENGTH_LONG).show(); 
      } 
     }); 

    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     callbackManager.onActivityResult(requestCode, resultCode, data); 
    } 

    private void handleFacebookAccessToken(AccessToken token) { 


     AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken()); 
     mAuth.signInWithCredential(credential) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         if (task.isSuccessful()) { 
          // Sign in success, update UI with the signed-in user's information 

          FirebaseUser user = mAuth.getCurrentUser(); 
          Intent intent=new Intent(getApplicationContext(),NextActivity.class); 
          // intent.putExtra("NAME",user.getDisplayName()); 
          startActivity(intent); 
          finish(); 

         } else { 
          // If sign in fails, display a message to the user. 

          Toast.makeText(getApplicationContext(), "Authentication failed.", 
            Toast.LENGTH_SHORT).show(); 

         } 

         // ... 
        } 
       }); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     // Check if user is signed in (non-null) and update UI accordingly. 
     FirebaseUser currentUser = mAuth.getCurrentUser(); 
//  currentUser.getDisplayName(); 
    } 

} 

我正在使用Firebase的Facebook登錄。我可以登錄,如果登錄成功,我將重定向到NextActivity。Android Firebase Facebook登錄顯示退出按鈕,當應用程序重新打開時

但是當我按下Facebook登錄按鈕時,它會顯示我幾秒鐘的註銷按鈕,然後將我重定向到NextActivity。當我關閉我的應用程序並再次打開它時,它會通過註銷按鈕向我顯示MainActivity。

我想,如果應用程序被關閉並重新打開再次

+0

你想只顯示MainActivity一次嗎? –

+0

是的我的主要活動有Facebook登錄按鈕。一旦用戶成功簽名,我想顯示NextActivity.Even當應用程序關閉並重新打開它應該直接顯示NextActivity.Instead它顯示登錄後登錄文本相同的登錄按鈕。 – Pritish

回答

1

您可以從Facebook在登錄成功後保存AUTH Token你應用程序的Shared Preferences settings,然後當你開始你的應用程序,你可以檢查是否有任何令牌保存在存儲器中,如果這意味着用戶已經登錄並直接將其重定向到下一頁或配置文件頁面。

這裏是小片段你可以做什麼:

final static String PREFS_NAME = "AUTH" 

public class MainActivity extends AppCompatActivity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 
     setContentView(R.layout.activity_main); 

     // check if user is already logged in 
     // i.e. auth token is present or not 
     String token = settings.getString("auth_token", null); 
     // means user is logged in token was found 
     if (token != null) { 
      AUTH_TOKEN = token; 
      startActivity(new Intent(MainActivity.this, ProfileActivity.class)); 
     } 
    } 

     // YOUR REST OF THE CODE .... 

}); 

@Override 
public void onComplete(@NonNull Task<AuthResult> task) { 
    if (task.isSuccessful()) { 
     // Sign in success, update UI with the signed-in user's information 

     FirebaseUser user = mAuth.getCurrentUser(); 

     // SAVE THE USER DETAILS OR PART OF IT IN SHARED PREFS 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putString("USER", user); 
     editor.apply(); 

     Intent intent=new Intent(getApplicationContext(),NextActivity.class); 
     // intent.putExtra("NAME",user.getDisplayName()); 
     startActivity(intent); 
     finish(); 

    } else { 
     // If sign in fails, display a message to the user. 

     Toast.makeText(getApplicationContext(), "Authentication failed.", 
       Toast.LENGTH_SHORT).show(); 

    } 

    // ... 
} 

這將工作,因爲每次啓動您的應用程序在onCreate方法檢查該AUTH文件,如果它的存在,將用戶直接發送到nextActivity

0

您可以使用共享偏好的目的,它顯示NextActivity。

所以在你LoginActivity的OnCreate做到這一點:

SharedPreferences  settings=getSharedPreferences("prefs",0); 
boolean firstRun=settings.getBoolean("firstRun",false); 
if(firstRun==false)//if running for first time 

{ 
    //Set the firstRun value to true 
    editor.putBoolean("firstRun",true); 
    editor.commit(); 
    //Continue your login process 

} 
else 
{ 
    // launch NextActivity if app is opened not for the first time 
    Intent i=new Intent(check.this, NextActivity.class); 
    startActivity(I); 
    finish(); 
} 
+0

仍然是同樣的問題。 – Pritish

+0

看看這個答案https://stackoverflow.com/questions/18612824/how-to-run-an-activity-only-once-like-splash-screen –

相關問題