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

时间:2016-05-13 19:00:13

标签: java android

我有这个代码,如果第一次启动应用程序。如果它是第一次启动,它会显示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();
        }

上面的代码工作正常但如果用户没有单击注册活动中的按钮并再次启动应用程序,则会将用户带到MainActivity,从而用户没有注册但使用该应用程序。如何在第一次启动时检查注册活动中的按钮,如果没有,则将用户导回到注册活动。

用户注册的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" />

更新代码

@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();
            }

        }

有没有办法可以检查点击注册活动中的按钮

1 个答案:

答案 0 :(得分:0)

不确定。当应用程序打开时,不是保存isFirstRun布尔值,而是在单击注册按钮时保存它。只需将逻辑移入注册按钮的onClick方法即可。但是,您仍需要在onCreate活动的Register方法中检查布尔值。像这样:

@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();
    }
    // .....

}

然后,在onClick

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