[英]Android: Start different activity if there is a user logged in
I am building an Android application where one activity is a login screen.我正在构建一个 Android 应用程序,其中一个活动是登录屏幕。 When the app is opened, if a user has already logged in, I would like to skip the LoginActivity and have the user be directed to another one.当应用程序打开时,如果用户已经登录,我想跳过 LoginActivity 并将用户定向到另一个。 When a user logs into my app (using Google Firebase), I save their username and other data in their device's shared preferences.当用户登录我的应用(使用 Google Firebase)时,我将他们的用户名和其他数据保存在他们设备的共享首选项中。 When they log out, their shared preferences are cleared.当他们注销时,他们的共享首选项将被清除。
The way I currently have my manifest file is such that the only activity that can be opened when the app is started is the LoginActivity:我目前拥有清单文件的方式是,启动应用程序时唯一可以打开的活动是 LoginActivity:
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In the LoginActivity's OnCreate() method, if there is a username saved in the shared preferences (meaning a user is logged in), I immediately change activities:在 LoginActivity 的 OnCreate() 方法中,如果共享首选项中保存了用户名(意味着用户已登录),我会立即更改活动:
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences userData = getApplicationContext().
getSharedPreferences("userdata", 0);
String n = userData.getString("username", "");
if (!userData.getString("username", "").equals(""))
{
Intent myIntent = new Intent(LoginActivity.this, TabbedActivity.class);
startActivity(myIntent);
}
}
However, there is a problem with this approach.但是,这种方法存在问题。 Many times, the LoginActivity is still shown for a split second before starting the TabbedActivity.很多时候,在启动 TabbedActivity 之前,LoginActivity 仍会显示一秒钟。 I would like to fix this so that the LoginActivity is actually never seen at all if a user is logged in.我想解决这个问题,以便在用户登录时实际上根本看不到 LoginActivity。
I assume that the approach I'm taking is all wrong and there is a much cleaner way of doing it such that the correct activity is immediately opened.我认为我采取的方法都是错误的,并且有一种更简洁的方法可以立即打开正确的活动。 Any help on this would be greatly appreciated.对此的任何帮助将不胜感激。
A possible approach:一种可能的方法:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:statusBarColor">@color/background_splash</item>
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="@color/background_splash"/>
</item>
<item>
<bitmap
android:src="@drawable/ic_splash_logo"
android:gravity="center"/>
</item>
</layer-list>
SplashTheme
as your application/launcher-activity theme:在您的清单中,将SplashTheme
设置为您的应用程序/启动器活动主题:<application
...
android:theme="@style/SplashTheme">
<!-- or -->
<activity
...
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
LoginActivity
in onCreate()
set your normal app theme and content view if the user is not logged in and also set app theme in the MainActivity
(or set it in the Manifest)如果用户未登录,则在onCreate()
LoginActivity
中设置正常的应用主题和内容视图,并在MainActivity
设置应用主题(或在清单中设置)public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
val isUserLoggedIn = ...
if (!isUserLoggedIn)
{
setTheme(R.style.AppTheme)
setContentView(R.layout.activity_login)
}
else {
//Navigate to Main Activity
}
}
}
I'm not sure if this is the best approach, but you could create a Loading activity that start the activity needed in any situation like.我不确定这是否是最好的方法,但是您可以创建一个 Loading 活动来启动在任何情况下所需的活动。
public class LoadingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences userData = getApplicationContext().
getSharedPreferences("userdata", 0);
String n = userData.getString("username", "");
if (!userData.getString("username", "").equals(""))
{
Intent myIntent = new Intent(LoginActivity.this, TabbedActivity.class);
startActivity(myIntent);
}else{
Intent myIntent = new Intent(OtherActivity.this, TabbedActivity.class);
startActivity(myIntent);
}
}
And about the view add a gif or a logo meanwhile.关于视图,同时添加 gif 或徽标。
I have an implementation, but no shared preferences, and I don't see the login screen at any time.我有一个实现,但没有共享首选项,而且我在任何时候都看不到登录屏幕。
My structure is as follows: I have an initial splash screen, then a main activty, which is where I override the onStart method to check if the user has an open section if it is passed to the home activity.我的结构如下:我有一个初始启动画面,然后是一个主要活动,我在这里重写 onStart 方法以检查用户是否有一个打开的部分,如果它被传递给主活动。
I hope it helps you, if you don't tell me and I'll add more code.我希望它可以帮助你,如果你不告诉我,我会添加更多的代码。
public override fun onStart() {
super.onStart()
// if user is loged goto Home Activity
if (firebaseUser != null) {
// Name, email address, and profile photo Url
val name = firebaseUser?.displayName
val email = firebaseUser?.email
val photoUrl = firebaseUser?.photoUrl
val uid = firebaseUser?.uid
val emailVerified = firebaseUser!!.isEmailVerified
goToActivity<HomeActivity>()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.