简体   繁体   English

关于首次使用Android中的共享首选项打开登录屏幕

[英]Regarding opening login screen for first time using shared preferences in android

I have referred to many of the answers but still my app does not show the registration screen when the app opens for first time. 我已经提到了很多答案,但是当我的应用程序首次打开时,我的应用程序仍未显示注册屏幕。 I have made use of shared preferences. 我利用了共享的首选项。 Any help would be appreciated.Thanks! 任何帮助将不胜感激。谢谢!

MainActivity.java: MainActivity.java:

public class MainActivity extends AppCompatActivity  {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String name;
        SharedPreferences prefs = this.getSharedPreferences( "prefs", MODE_PRIVATE);
         boolean firstStart=prefs.getBoolean("firstStart",true);

        if (firstStart) {
            //show start activity
             showRegistration();


        }

    }

    private void showRegistration() {

        SharedPreferences prefs=getSharedPreferences("prefs",MODE_PRIVATE);
        SharedPreferences.Editor editor=prefs.edit();
        editor.putBoolean("firstStart",false);
        editor.apply();
        Intent intent =new Intent(MainActivity.this,RegisterActivity.class);

    }


}

RegistrationActivity.java: RegistrationActivity.java:

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{

            private static final String TAG = "Main Activity";
            Firebase mRootRef;
            FirebaseAuth mAuth;
            EditText mname;
            Button mlogin;
            Button msignin;
            Button mregister;
            EditText memail;
            EditText maddress;
            EditText mconfirmpassword; EditText mpwd;
    private void updateUI(FirebaseUser user){
            if (user !=null){
              Toast.makeText(RegisterActivity.this,"Registration successful",Toast.LENGTH_SHORT).show();
               startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                             }              }

    protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_register);
           mRootRef=new Firebase("https://goorgano-8bdfe.firebaseio.com/Users");
           msignin=(Button)findViewById(R.id.sign_in_admin);
           mAuth = FirebaseAuth.getInstance();
            mname=(EditText)findViewById(R.id.username);
            mlogin=(Button)findViewById(R.id.login);
            memail=(EditText)findViewById(R.id.email);
            maddress=(EditText)findViewById(R.id.address);
            mconfirmpassword=(EditText)findViewById(R.id.confirmpassword);
            mpwd= (EditText)findViewById(R.id.password);
            mregister=(Button)findViewById(R.id.register);
            mlogin.setOnClickListener(this);
            msignin.setOnClickListener(this);
            mregister.setOnClickListener(this);


    }

    public void onClick(View v)
    {
        int i=v.getId();
        if(i==R.id.sign_in_admin)
        {

        }
        else if(i==R.id.login)
        {
            startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
        }
        else if(i==R.id.register)
        {
            createAccount();
            startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
        }
    }
    private void createAccount()
    {
        Log.e(TAG, "createAccount:" + memail.getText().toString());
        if (!validateForm(memail.getText().toString(),mpwd.getText().toString(),maddress.getText().toString(),mname.getText().toString()))
            {
                return;
            }
               String value=mname.getText().toString();
               String key="name";
                Firebase childref=mRootRef.child(value);Firebase c=childref;
                childref.setValue(value);
                value=memail.getText().toString();
                String email=value;
                key="email";
                childref=childref.child(key);
                childref.setValue(value);
                value=maddress.getText().toString();
                String pwd=mpwd.getText().toString();
                key="address";
                childref=c.child(key);
                childref.setValue(value);
                mAuth.createUserWithEmailAndPassword(email,pwd)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
                         {
                    @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    if (task.isSuccessful()) {
                                        Log.e(TAG, "createAccount: Success!");

                                        // update UI with the signed-in user's information
                                        FirebaseUser user = mAuth.getCurrentUser();
                                        updateUI(user);
                                    } else {
                                        Log.e(TAG, "createAccount: Fail!", task.getException());
                                        Toast.makeText(getApplicationContext(), "Authentication failed!", Toast.LENGTH_SHORT).show();
                                        updateUI(null);
                                    }
                                                                                        }
                          });
    }

    private boolean validateForm(String email, String password, String address, String username)
      {

        if (TextUtils.isEmpty(email))
        {
            Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
            return false;
        }
        if(!Patterns.EMAIL_ADDRESS.matcher(email).matches())
        {
            Toast.makeText(getApplicationContext(), "Enter valid email address!", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(password))
        {
            Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
            return false;
        }

        if (TextUtils.isEmpty(username))
        {
            Toast.makeText(getApplicationContext(), "Enter username!", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(address))
        {
            Toast.makeText(getApplicationContext(), "Enter address!", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (password.length() < 6)
        {
            Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }


}

Update your onCreate with this 以此更新您的onCreate

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String name;
        SharedPreferences prefs = this.getSharedPreferences( "prefs", MODE_PRIVATE);
         boolean firstStart=prefs.contains("firstStart");

        if (!firstStart) {
            //show start activity
             showRegistration();


        }

    }

And let me know it worked 让我知道它有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于android中的共享首选项 - Regarding shared preferences in android 使用共享首选项一次登录? - One time login using shared preferences? 我想使用共享的首选项在android中实现一次注册屏幕,但我无法获取以前的共享首选项 - I want to implement one time registration screen in android using shared preferences but i'm not able to get the previous shared preferences Android使用共享的首选项检查首次运行 - Android using shared preferences to check on first run 使用谷歌+登录屏幕并使用共享首选项来跟踪android中的登录用户 - login screen with google+ and using shared preferences to track logged-in user in android Android应用程序登录屏幕-错误的变量和共享首选项比较 - Android app login screen - wrong variable and shared preferences comparison 启动画面-首次使用共享首选项 - Splashscreen - first Time with shared preferences 如何在 Android Studio 中首次使用 xml 文件初始化共享首选项值 - How to initialize the Shared Preferences values ​using an xml file for the first time in Android Studio 使用共享首选项的 Android 登录/注册 - Android login/registration with shared preferences Android开发:关于共享首选项的使用 - Android Development: Regarding the usage of shared preferences
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM