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

huangapple go评论86阅读模式
英文:

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:

        &lt;activity android:name=&quot;.LoginActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;

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(&quot;userdata&quot;, 0);
        String n = userData.getString(&quot;username&quot;, &quot;&quot;);
        if (!userData.getString(&quot;username&quot;, &quot;&quot;).equals(&quot;&quot;))
        {
            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

可能的方法:

  1. 创建一个闪屏主题的样式:
<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. 为闪屏创建一个背景可绘制资源(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. 在清单文件中将 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>
  1. 在你的 LoginActivityonCreate() 方法中,如果用户未登录,设置你的正常应用主题和内容视图,并在 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:

  1. Create a style for splash theme:
&lt;style name=&quot;SplashTheme&quot; parent=&quot;Theme.AppCompat.NoActionBar&quot;&gt;
    &lt;item name=&quot;android:statusBarColor&quot;&gt;@color/background_splash&lt;/item&gt;
    &lt;item name=&quot;android:windowBackground&quot;&gt;@drawable/background_splash&lt;/item&gt;
&lt;/style&gt;
  1. Create a background drawable (drawable/background_splash.xml) for splash:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;layer-list xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;item&gt;
        &lt;color android:color=&quot;@color/background_splash&quot;/&gt;
    &lt;/item&gt;
    &lt;item&gt;
        &lt;bitmap
            android:src=&quot;@drawable/ic_splash_logo&quot;
            android:gravity=&quot;center&quot;/&gt;
    &lt;/item&gt;
&lt;/layer-list&gt;
  1. In your manifest, set the SplashTheme as your application/launcher-activity theme:
&lt;application
    ...
    android:theme=&quot;@style/SplashTheme&quot;&gt;

&lt;!-- or --&gt;

&lt;activity
    ...
    android:theme=&quot;@style/SplashTheme&quot;&gt;
    &lt;intent-filter&gt;
        &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

        &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
    &lt;/intent-filter&gt;
&lt;/activity&gt;

  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)
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

答案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(&quot;userdata&quot;, 0);
        String n = userData.getString(&quot;username&quot;, &quot;&quot;);
        if (!userData.getString(&quot;username&quot;, &quot;&quot;).equals(&quot;&quot;))
        {
            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&lt;HomeActivity&gt;()
    }
}

huangapple
  • 本文由 发表于 2020年9月15日 02:10:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63889686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定