이미 로그인한 경우 로그인 활동을 적절하게 건너뜁니다.
-
21-12-2019 - |
문제
내 실행기 아이콘이 현재 로그인 활동을 시작합니다.SharedPreferences에 로그인 상태를 저장했습니다.방법이 있나요? 제대로 로그인 활동을 건너뛰고 로그인 없이 바로 기본 활동으로 이동합니다. 어느 UI 결함.관련된 모든 기존 솔루션 finish()
~에 onCreate()
로그인 활동 제목이 잠시 표시되거나 기타 간단한 빈 화면 UI 결함이 발생합니다.
해결책
MainActivity 또는 LoginActivity를 열기로 결정하는 UI가 없는 실행 프로그램 활동이 있습니다.다음을 사용하여 UI 없음을 선언할 수 있습니다.
android:theme="@android:style/Theme.NoDisplay"
다른 두 가지 가능한 솔루션:
다른 방법으로 수행하십시오.mainActivity를 런처로 만들고 사용자가 로그인했는지 확인하도록 하세요.그런 다음 그렇지 않은 경우 loginActivity로 리디렉션합니다.
또 다른 방법은 조각으로 작업하는 것입니다.mainFragment와 loginFragment를 모두 로드할 수 있는 기본 활동이 있어야 합니다.참고로: https://developer.android.com/training/basics/fragments/index.html
다른 팁
사용자의 사용자 이름과 비밀번호가 이미 있는지 확인하는 기본 활동을 생성할 수 있습니다. SharedPreferences
존재하는 경우 활동을 시작하므로 그렇지 않습니다.
예:
public class BeanStalkBaseActivity extends SherlockActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(SavedPreference.getUserName(this).length() == 0)
{
Intent intent = new Intent(this,LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
}else
{
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
}
}
}
BeanStalkBase활동 런처는 검사기 역할만 하므로 런처여야 합니다.
스플래시 화면 활동 중에 로그인 상태를 확인할 수도 있습니다.시작 화면은 앱이 로드될 때 앱이 멈추지 않았음을 사용자에게 알려주는 데 유용하며 앱을 적절한 화면으로 리디렉션하는 데에도 사용할 수 있습니다.
나는 처음으로 하나를 만들 때 이 훌륭한 가이드를 따랐습니다. https://www.bignerdranch.com/blog/splash-screens-the-right-way/
사용자가 기본 액티비티나 현재 액티비티에 이미 로그인되어 있는지 확인하고 로그인되어 있으면 다른 액티비티로 전환하면 UI 결함이 발생합니다.현재 활동이 1~2초 동안 표시된 다음 대상 활동으로 전환됩니다.
다음과 같이 할 수 있습니다:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null) {
Toast.makeText(MainActivity.this, "Already Logged In",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
} else {
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
BtnSignUp = findViewById(R.id.btnSignUp);
BtnLogIn = findViewById(R.id.btnLogIn);
BtnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent signUp = new Intent(MainActivity.this, SignUpActivity.class);
startActivity(signUp);
}
});
BtnLogIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent logIn = new Intent(MainActivity.this, Login.class);
startActivity(logIn);
}
});
}
}
주요 활동에서 사용자가 null이 아닌지 확인한 다음 집으로 돌아갑니다.
firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
finish();
startActivity(new Intent(MainActivity.this, UserHomeActivity.class));
}