简体   繁体   English

检查首次启动android时是否在活动上单击了一个按钮

[英]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. 如果它是第一次启动,它会显示Register活动,然后在第二次启动时显示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. 上面的代码工作正常但如果用户没有单击注册活动中的按钮并再次启动应用程序,则会将用户带到MainActivity,从而用户没有注册但使用该应用程序。 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 用于用户注册的xml

<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. 应用程序打开时,不要保存isFirstRun布尔值,而是在单击注册按钮时保存它。 Just move the logic into the registration button's onClick method. 只需将逻辑移动到注册按钮的onClick方法即可。 However, you still need to check for the boolean in the onCreate method of your Register activity. 但是,您仍需要在Register活动的onCreate方法中检查布尔值。 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 : 然后,在你的onClick

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

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

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