英文:
Android: Start different activity if there is a user logged in
问题
我正在构建一个Android应用程序,其中一个活动是登录界面。当打开应用程序时,如果用户已经登录,我希望跳过登录界面(LoginActivity),并将用户引导到另一个界面。当用户登录我的应用程序(使用Google Firebase),我会将他们的用户名和其他数据保存在他们设备的共享偏好中。当他们登出时,他们的共享偏好会被清除。
我当前在清单文件中的设置是,只有在启动应用程序时才能打开登录界面(LoginActivity):
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
在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);
}
}
然而,这种方法存在一个问题。很多时候,在启动TabbedActivity之前,LoginActivity仍会短暂地显示出来。我希望修复这个问题,确保如果用户已登录,实际上根本不会看到LoginActivity。
我认为我采取的方法可能是错误的,有一种更清晰的方式可以立即打开正确的活动。非常感谢您对此的任何帮助。
英文:
I am building an Android application where one activity is a login screen. 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. When a user logs into my app (using Google Firebase), I save their username and other data in their device's shared preferences. 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:
<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:
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. I would like to fix this so that the LoginActivity is actually never seen at all if a user is logged in.
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.
答案1
得分: 2
可能的方法:
- 创建一个闪屏主题的样式:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:statusBarColor">@color/background_splash</item>
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
- 为闪屏创建一个背景可绘制资源(drawable/background_splash.xml):
<?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
设置为你的应用/启动活动的主题:
<application
...
android:theme="@style/SplashTheme">
<!-- 或者 -->
<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
的onCreate()
方法中,如果用户未登录,设置你的正常应用主题和内容视图,并在MainActivity
中设置应用主题(或在清单文件中设置):
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean isUserLoggedIn = ...;
if (!isUserLoggedIn) {
setTheme(R.style.AppTheme);
setContentView(R.layout.activity_login);
} else {
// 跳转到主活动
}
}
}
英文:
A possible approach:
- Create a style for splash theme:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:statusBarColor">@color/background_splash</item>
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
- Create a background drawable (drawable/background_splash.xml) for splash:
<?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>
- In your manifest, set the
SplashTheme
as your application/launcher-activity theme:
<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>
- In your
LoginActivity
inonCreate()
set your normal app theme and content view if the user is not logged in and also set app theme in theMainActivity
(or set it in the Manifest)
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
}
}
}
答案2
得分: 1
我不确定这是否是最佳方法,但你可以创建一个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);
}
}
}
关于视图方面,可以添加一个gif动画或徽标来提升用户体验。
英文:
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.
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.
答案3
得分: 0
我有一个实现,但没有共享偏好设置,并且在任何时候都看不到登录界面。
我的结构如下:我有一个初始的闪屏界面,然后是一个主活动,在这个活动中,我重写了onStart方法,以检查用户是否有一个打开的部分,如果传递给主页活动。
希望对您有帮助,如果没有告诉我,我会添加更多的代码。
public override fun onStart() {
super.onStart()
// 如果用户已登录,则跳转到主页活动
if (firebaseUser != null) {
// 用户名、电子邮件地址和个人资料照片网址
val name = firebaseUser?.displayName
val email = firebaseUser?.email
val photoUrl = firebaseUser?.photoUrl
val uid = firebaseUser?.uid
val emailVerified = firebaseUser!!.isEmailVerified
goToActivity<HomeActivity>()
}
}
英文:
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.
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>()
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论