Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 938
AI. Avatar asked Dec 13 '16 09:12

AI.


1 Answers

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
            }
        }
    }
}
like image 72
Nainal Avatar answered Oct 30 '22 22:10

Nainal