简体   繁体   中英

Check if a button is clicked on an activity on first launch android

I have this code that if an app is launched the first time. If it is launched the first time, it displays the Register activity then on second launch it displays the MainAcitivity.

Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("isFirstRun", false);
        if (isFirstRun) {

            startActivity(new Intent(Register.this, MainActivity.class));
            //Toast.makeText(Register.this, "Authenticated", Toast.LENGTH_LONG) .show();
            //finish the application after first run
            finish();
        }
        {
        getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("isFirstRun", true).commit();
           // finish();
        }

The above code works fine but if the user did not click the button in the register activity and launches the app once again, it takes the user to MainActivity thereby the user did not register but making use of the app. How can I check that the button in the register activity is clicked on first launch and if not navigate the user back to the register activity.

xml for user registration

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/phone"
        android:layout_centerVertical="true"
        android:text="Username"/>

    <EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:ems="10" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/username"
        android:layout_below="@+id/username"
        android:text="Phone No" />


    <EditText
        android:id="@+id/phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView01"
        android:layout_centerHorizontal="true"
        android:ems="10"/>

    <Button
        android:id="@+id/register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/phone"
        android:layout_below="@+id/phone"
        android:text="Register" />

updated code

@Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.register) {


//          if(!validate()){
            // call AsynTask to perform network operation on separate thread
            new HttpAsyncTask().execute("http://xyz/create");
            //new AttemptRegistration().execute();
//          }

            Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("isFirstRun", false);
            if (isFirstRun) {
                //show start activity

                startActivity(new Intent(Register.this, MainActivity.class));
                //Toast.makeText(Register.this, "Authenticated", Toast.LENGTH_LONG) .show();
                //finish the application after first run
                finish();
            }
            {
            getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("isFirstRun", true).commit();
               // finish();
            }

        }

Is there a way I can place a check that the button in the register activity is clicked

Sure. Instead of saving your isFirstRun boolean when the app opens, save it when the registration button is clicked. Just move the logic into the registration button's onClick method. However, you still need to check for the boolean in the onCreate method of your Register activity. Like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ....

    Boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("isFirstRun", false);
    if (isFirstRun) {
        //show start activity

        startActivity(new Intent(Register.this, MainActivity.class));
        //Toast.makeText(Register.this, "Authenticated", Toast.LENGTH_LONG) .show();
        //finish the application after first run
        finish();
    }
    // .....

}

Then, in your onClick :

@Override
public void onClick(View v) {
    getSharedPreferences("PREFERENCE", MODE_PRIVATE).edit().putBoolean("isFirstRun", true).commit();
// ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM