如何在setupActivity之后继续使用mainActivity?

时间:2017-03-19 18:43:19

标签: android firebase firebase-realtime-database firebase-authentication

在loginActivity之后,我的应用程序进入setupActivity,用户需要在其中输入他的名字,并且有一个提交按钮,将名称上传到firebase,然后它应该进入mainActivity,这应该只对用户发生一次。问题是mSubmitbtn将名称存储到数据库,但没有继续进行mainActivity,并且setupActivity再次弹出。

public class SetupActivity extends AppCompatActivity {
private EditText mNameField;
private Button mSubmitBtn;
private DatabaseReference mDatabaseUsers;
private FirebaseAuth mAuth;
private ProgressDialog mProgress;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setup);
    mNameField = (EditText) findViewById(R.id.setupNameField);
    mSubmitBtn = (Button) findViewById(R.id.setupSubmitBtn);
    mProgress = new ProgressDialog(this);
    mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");
    mAuth = FirebaseAuth.getInstance();


    mSubmitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startSetupAccount();
        }
    });
}


private void startSetupAccount() {

    final String user_id = mAuth.getCurrentUser().getUid();

    final String name = mNameField.getText().toString().trim();

    if (!TextUtils.isEmpty(name)) {

        mProgress.setMessage("Finishing setup.");
        mProgress.show();


        mDatabaseUsers.child(user_id).child("name").setValue(name);


        mProgress.dismiss();

        Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
        mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(mainIntent);
    }
    }

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

1 个答案:

答案 0 :(得分:3)

在“共享首选项”中设置一个值,用于跟踪应用程序是否首次运行。这可以放在onCreateonStart

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstRun = sharedPreferences.getBoolean("isFirstRun", true);

if (isFirstRun) {
    //Set value to false, so does not repeat
    sharedPreferences.edit().putBoolean("isFirstRun", false);
    //show login activity
    startActivity(new Intent(MainActivity.this, LoginActivity.class), REQUEST_FIRST_LAUNCH);
}