简体   繁体   English

Android:如果有用户登录,则启动不同的活动

[英]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:一种可能的方法:

  1. 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>
  1. Create a background drawable (drawable/background_splash.xml) for splash:为飞溅创建一个背景可绘制对象(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>
  1. In your manifest, set the 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>

  1. In your 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
        }
    }
}

Splash screen reference启动画面参考

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.

相关问题 如果用户第二次登录,则使用 Firebase 打开一个不同的活动 - Using Firebase open a different activity if user logged in second time ANDROID:从2个不同的Activity(不同的Intent)开始相同的Activity - ANDROID: Start same Activity from 2 different Activity (different Intent) 显示有关Android Studio中其他活动的登录用户数据 - Show logged in user data on another activity in Android Studio Android。检查用户是否已登录并打开菜单活动 - Android.Check if user is logged and open Menu activity Android 导航组件 - 如果用户未登录,则导航到其他活动 - Android Navigation Component - Navigation to additional activity if user not logged in Android - 保持用户登录 - Android - Keep user logged in 无法从不同程序包中的活动启动android服务 - unable to start android service from activity that is in the different package 当用户键入特定单词时如何启动 android 活动 - how to start an android activity when ever a user types a particular words 在 Android 中的另一个活动中开始活动 - Start activity in another activity in Android 如何使用广播接收器在android中注销后防止用户继续以前的活动? - How to prevent user to go on previous activity after logged out in android using broadcast receiver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM