android - tag - webview onpagestarted not called




How to make the app go to another page if it is not run by first time (6)

I have RegisterPage and LoginPage. When the app is run, it will check whether the app is first time run or not in RegisterPage. If it is first time run and the save button is not clicked, it will in RegisterPage. If it is run second times but the save button is never clicked, it will remain in RegisterPage too. Otherwise it will go to LoginPage.

Here my updated code

Register

appGetFirstTimeRun();
boolean clicked=false;

 buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clicked=true;
                int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
                SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);
                appPreferences.edit().putInt("app_second_time",
                        appCurrentBuildVersion).apply();
                String name = editTextName.getText().toString();
                String pass = editTextPassword.getText().toString();
                String confirm = editTextConfirm.getText().toString();
                if ((editTextName.getText().toString().trim().length() == 0) || (editTextPassword.getText().toString().trim().length() == 0) || (editTextConfirm.getText().toString().trim().length() == 0)) {
                    Toast.makeText(getApplicationContext(), "Field cannot be null", Toast.LENGTH_LONG).show();
                } 
                else
                 {
                      insertData(name, pass, imageUri); // insert to SQLite
                      Intent intent = new Intent(MainActivity.this, AddMonthlyExpenses.class);
                      intent.putExtra("name", name);
                      startActivity(intent);
                  }
            }
        });

  private int appGetFirstTimeRun() {

        //Check if App Start First Time
        SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);
        int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
        int appLastBuildVersion = appPreferences.getInt("app_first_time", 0);

        if (appLastBuildVersion == appCurrentBuildVersion && clicked) {
            Intent intent = new Intent(MainActivity.this,LoginPage.class);
            startActivity(intent);
            return 1;
        } else {
            appPreferences.edit().putInt("app_first_time",
                    appCurrentBuildVersion).apply();
            if (appLastBuildVersion == 0) {
                Toast.makeText(getApplicationContext(), "First time", Toast.LENGTH_SHORT).show();
                return 0; //es la primera vez
            } else {
                return 2; //es una versión nueva
            }
        }
    }

The problem is when I click the save button and exit from the app. When I run the app again it still in the RegisterPage, not in LoginPage.


Check button click after inserting data into SQLite, So you can confirm that your data has successfully saved and you can proceed to next screen.

Find my comments in below code and edit your code:-

public class Register extends AppCompatActivity {
    Button buttonSave;
    boolean clicked=false;//remove this
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        appGetFirstTimeRun();//call this method here
        buttonSave=(Button)findViewById(R.id.button);
        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clicked=true;//remove this
                int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
                SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);
                appPreferences.edit().putInt("app_second_time", appCurrentBuildVersion).apply();

                String name = editTextName.getText().toString();
                String pass = editTextPassword.getText().toString();
                String confirm = editTextConfirm.getText().toString();
                if ((editTextName.getText().toString().trim().length() == 0) || (editTextPassword.getText().toString().trim().length() == 0) || (editTextConfirm.getText().toString().trim().length() == 0)) {
                    Toast.makeText(getApplicationContext(), "Field cannot be null", Toast.LENGTH_LONG).show();
                }
                else
                {
                    insertData(name, pass, imageUri); // insert to SQLite
                    appPreferences.edit().putBoolean("btn_clicked", true).apply();//add this line
                    Intent intent = new Intent(Register.this, AddMonthlyExpenses.class);
                    intent.putExtra("name", name);
                    startActivity(intent);
                }
            }
        });
    }
    private int appGetFirstTimeRun() {

        //Check if App Start First Time
        SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);
        int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
        int appLastBuildVersion = appPreferences.getInt("app_first_time", 0);
        boolean is_btn_click=appPreferences.getBoolean("btn_clicked",false);//add this line

        if ((appLastBuildVersion == appCurrentBuildVersion) && is_btn_click) { //edit this line like this
            Intent intent = new Intent(Register.this,LoginPage.class);
            startActivity(intent);
            return 1;
        } else {
            appPreferences.edit().putInt("app_first_time",
                    appCurrentBuildVersion).apply();
            if (appLastBuildVersion == 0) {
                Toast.makeText(getApplicationContext(), "First time", Toast.LENGTH_SHORT).show();
                return 0; //es la primera vez
            } else {
                return 2; //es una versión nueva
            }
        }
    }
}

I think this may be the answer. on your code clicked boolean variable is not stored on shared preferences but you are checking condition inside appGetFirstTimeRun() (button may not be clicked at second launch, but your condition needs to be true) so change your code by Adding this line appPreferences.edit().putBoolean("first_run", true).apply();on buttonSave clicklistener and then add this line clicked = appPreferences.getBoolean("first_run", false);on appGetFirstTimeRun() function and the complete code would be.

appGetFirstTimeRun();
boolean clicked=false;

 buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
                SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);
                appPreferences = appPreferences.edit().putBoolean("first_run",
                true).apply(); //***Add this ****
                appPreferences.edit().putInt("app_second_time",
                        appCurrentBuildVersion).apply();
                String name = editTextName.getText().toString();
                String pass = editTextPassword.getText().toString();
                String confirm = editTextConfirm.getText().toString();
                if ((editTextName.getText().toString().trim().length() == 0) || (editTextPassword.getText().toString().trim().length() == 0) || (editTextConfirm.getText().toString().trim().length() == 0)) {
                    Toast.makeText(getApplicationContext(), "Field cannot be null", Toast.LENGTH_LONG).show();
                } 
                else
                 {
                      insertData(name, pass, imageUri); // insert to SQLite
                      Intent intent = new Intent(MainActivity.this, AddMonthlyExpenses.class);
                      intent.putExtra("name", name);
                      startActivity(intent);
                  }
            }
        });

  private int appGetFirstTimeRun() {

        //Check if App Start First Time
        SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);
        int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
        int appLastBuildVersion = appPreferences.getInt("app_first_time", 0);
        clicked = appPreferences.getBoolean("first_run", false); //*** Add this ***
        if (appLastBuildVersion == appCurrentBuildVersion && clicked) {
            Intent intent = new Intent(MainActivity.this,LoginPage.class);
            startActivity(intent);
            return 1;
        } else {
            appPreferences.edit().putInt("app_first_time",
                    appCurrentBuildVersion).apply();
            if (appLastBuildVersion == 0) {
                Toast.makeText(getApplicationContext(), "First time", Toast.LENGTH_SHORT).show();
                return 0; //es la primera vez
            } else {
                return 2; //es una versión nueva
            }
        }
    }

You also need to change the shared preferences value on click of save button. Then only next time when you open the app appGetFirstTimeRun method will load the Login page.

In you btnSave click listener where you are starting intent for activity just before startActivity add this code

int appCurrentBuildVersion = BuildConfig.VERSION_CODE;
    SharedPreferences appPreferences = getSharedPreferences("MyAPP", 0);

   appPreferences.edit().putInt("app_first_time",
                appCurrentBuildVersion).apply();

add this code to your splash screen

 SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(context);
    boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", false);
    if (!isFirstRun) {
startActivity(new Intent(context,RegirstorActivity.class));
}else{
startActivity(new Intent(context,LoginActivty.class));
}

so basically it will decide Is it first time (user registered) or not, after registration update the SharedPreferences


you are depending on SharedPreferences as well as on clicked variable, you can depend on SharePreferences but not on variable because on each run you are setting clicked value to false.

1) Save current version in preference when button is clicked

appPreferences.edit().putInt("app_first_time",
                appCurrentBuildVersion).apply();

2) save clicked value in preference when button is clicked

appPreferences.edit().putBoolean("clicked",
                true).apply();

Now inside your appGetFirstTimeRun() fetch the value of version and clicked from SharedPreferences

int appLastBuildVersion = appPreferences.getInt("app_first_time", 0);
boolean clicked = appPreferences.getBoolean("clicked", false);

your are using click variable to identify if button is clicked or not but when you exist from the app and again then click value reset to false so instead of saving value in click variable you can use shared preference to save value true or false on button click and get value from shared preference to check

on click of save button

   appPreferences.edit().putInt("app_second_time",
                    appCurrentBuildVersion).apply();   




sharedpreferences