0

Hi then the activity below makes a simple login! With this acitivity I have these two problems on the code below

1) SharedPreferences: Once logged in the user the user credentials should remain saved for the next boot but this only works the first time when then the user closes the application for the second time the app does not maintain saved credentials

2) Saved Login Services: I would like to disable the request to save the passowrd and the username in the various services to keep the login data saved!

how can i solve these?

Java Code:

package app;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.json.JSONException;

import app.Controller.User;
import app.Service.Permissions;
import app.Service.Support;
import app.View.TabActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvUsername, tvPassword;
    private EditText txtUsername, txtPassword;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayUseLogoEnabled(true);

        //Disabilita da StrictMode di android solo su sdk superiori a 9
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        /* Istanza dei campi */
        tvUsername = (TextView) findViewById(R.id.usernametextview);
        txtUsername = (EditText) findViewById(R.id.usernameeditext);
        tvPassword = (TextView) findViewById(R.id.passwordtextview);
        txtPassword = (EditText) findViewById(R.id.passwordtext);
        final Button loginbutton = (Button) findViewById(R.id.buttonlogin);

        //Configuro la funzione Listener sul login button
        loginbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                // Code here executes on main thread after user presses button
                if (txtUsername.length() != 0 && txtPassword.length() != 0) {

                    if (Permissions.isOnline() == true) {
                        LoginFunction(txtUsername.getText().toString(), txtPassword.getText().toString(), true);
                    } else {
                        Support.Notification(MainActivity.this, "Errore", "Non stai navigando");
                    }
                }
            }
        });


        SharedPreferences sp1 = this.getSharedPreferences("Login", Context.MODE_PRIVATE);
        String userload = sp1.getString("Username", null);
        String pwdload = sp1.getString("Password", null);
        LoginFunction(userload, pwdload, false);

    }

    private void LoginFunction(String Username, String Password, Boolean ShowMessage) {
        User u = new User();
        Boolean ret = false;
        try {
            ret = u.Login(Username, Password);
            if (ret == true) {
                //Salvo le credenziali d'accesso in sharedPrefrences
                SharedPreferences sp = this.getSharedPreferences("Login", Context.MODE_PRIVATE);
                SharedPreferences.Editor Ed = sp.edit();
                Ed.putString("Username", txtUsername.getText().toString());
                Ed.putString("Password", txtPassword.getText().toString());
                Ed.commit();
                //Creo un oggetto Intent da inviare ad una activity
                Intent tabhome = new Intent(MainActivity.this, TabActivity.class);
                tabhome.putExtra("User", u);
                startActivity(tabhome);
            } else {
                if (ShowMessage == true) {
                    Support.Notification(MainActivity.this, "Errore", "Login non riuscito");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }


}
riki
  • 1,502
  • 5
  • 17
  • 45

1 Answers1

1

The problem why your values is not stored properly is:

Ed.commit();

replace it with

Ed.apply();

What is the problem with second 2)?

// Edit:

Check my working code for activity:

SharedPreferences prefs = getSharedPreferences(MyConstant.SHARED_PREFERENCES_NAME, MODE_PRIVATE);

if (prefs.getBoolean("firstTimeRun", true)) {
    prefs.edit().putBoolean("firstTimeRun", false).apply();

    // do something for the first time
}
MarcinR
  • 786
  • 1
  • 6
  • 16
  • The first problem also persists with ed.apply () should keep the data even after the application is closed, in problem 2 I would like to disable the request to save passowrd from the google services! – riki Jul 10 '18 at 08:43
  • You have some solutions – riki Jul 10 '18 at 10:46
  • @DennyDogOk now the first is correct thanks! Do you have any idea how to fix the second? – riki Jul 10 '18 at 11:00
  • I'm glad to help. I don't understand your second question clearly. What kind of save password services? Please describe your use case and any example :) – MarcinR Jul 10 '18 at 11:09
  • When I install the application on some devices for example samsung when the user fill in the username and password field the app asks them to save that information in the Samsung Accout, I would like to disable this function when the user uses my application – riki Jul 10 '18 at 12:49
  • @riki maybe this thread will helps you https://stackoverflow.com/questions/45731372/disabling-android-o-auto-fill-service-for-an-application – MarcinR Jul 10 '18 at 13:05