什么是实现一个登录(验证)屏幕中的最佳做法? [英] What is the best practice to implement a login (auth) screen?

查看:93
本文介绍了什么是实现一个登录(验证)屏幕中的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现登录屏幕在我的应用程序,并寻找最佳的做法。
假设此code:

I want to implement login screen in my app and looking for the best practice. Suppose this code:

public class LoginActivity extends Activity {
    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        setContentView(R.layout.activity_login);
        Button btnLogin=(Button)findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
            }
        });
    }
}

public class MainActivity extends Activity {
    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);
        Button btnLogout=(Button)findViewById(R.id.btnLogout);
        btnLogout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
                startActivity(new Intent(getApplicationContext(), LoginActivity.class));
            }
        });
    }  
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    <application android:icon="@drawable/icon" android:label="@string/app_name">

      <activity   android:name=".LoginActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
      </activity>
      <activity   android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
      </activity>
    </application>
</manifest>

这将为第一个应用程序运行的工作,我会记住的密码。但下一次,当密码存储应用程序已经存在,我不希望显示LoginActivity。所以,唯一的解决办法我有:

That's will work for the first app run, I will remember password. But next time, when password already exists in app storage, I don't want to show LoginActivity. So, the only solution I have:

public class LoginActivity extends Activity {
    public void onCreate(Bundle icicle){
        super.onCreate(icicle);
        setContentView(R.layout.activity_login);
        Button btnLogin=(Button)findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
            }
        });
        //consider that passwodExists() looks for credentials
        if(passwodExists()){
           finish();
           startActivity(new Intent(getApplicationContext(), MainActivity.class));
        }
    }
}

是可以接受的?


  • 你会用什么这样的问题?

  • 是安全的启动活动,或从的onCreate显示对话框()?

推荐答案

我觉得没关系,只要你考虑的主要布局onResume后退按钮()和关闭应用程序,如果没有一个有效的登录。

I think that's OK as long as you take into consideration the back button in the main layouts onResume() and close the application if there isn't a valid login.

当我这样做,我做了决定要到登录活动或为主要业务,并确信,闪屏活性不背历史任务的一部分,闪屏类型的活动。这样,当再砸在登录活动后退按钮,它会玩完的应用程序。

When I did this I made a splash screen type activity that determined to go to the Login activity or to the Main activity and made sure that the splash screen activity was not part of the back task history. That way when then hit the back button on the login activity, it would finish off the application.

这篇关于什么是实现一个登录(验证)屏幕中的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆