firebase导航以在家庭活动中切换活动时登录活动

时间:2018-04-07 15:39:41

标签: java android firebase

我已完成Firebase身份验证,该身份验证登录用户并导航到HomeActivity。这可以工作,但是当我尝试从Home Activity移动到下一个时,它会导航回MainActivity(在这种情况下是登录)。

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnSearch;
    private TextView welcomeLbl;
    private Button btnSignout;

    FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        btnSearch = (Button) findViewById(R.id.btnSearch);
        welcomeLbl = (TextView) findViewById(R.id.welcomeLbl);
        btnSignout = (Button) findViewById(R.id.btnSignout) ;

        btnSearch.setOnClickListener(this);
        btnSignout.setOnClickListener(this);

        mAuth = FirebaseAuth.getInstance();

        if (mAuth.getCurrentUser() == null) {

            Intent signInIntent = new Intent(HomeActivity.this, MainActivity.class);
            startActivity(signInIntent);
            finish();

        } else {

            welcomeLbl.setText("You are logged in!");
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnSearch:
                Intent intent = new Intent(HomeActivity.this, SearchActivity.class);
                startActivity(intent);

            case R.id.btnSignout:
                mAuth.signOut();
                Intent signInIntent = new Intent(HomeActivity.this, MainActivity.class);
                startActivity(signInIntent);
                finish();
        }
    }
}

我的退出按钮的案例有效,因为它会按预期退出并导航回登录活动,但按钮搜索会执行相同的操作,而应该导航到搜索活动。

这是我的搜索活动:

public class SearchActivity extends AppCompatActivity {

    private EditText searchField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        searchField = (EditText) findViewById(R.id.searchField);

    }
}

1 个答案:

答案 0 :(得分:2)

尝试在switch选项语句中添加break,如果你没有添加break,代码将继续作为fall through执行,当你想要一个选项来执行另一个选项的操作时。

case R.id.btnSearch:
            Intent intent = new Intent(HomeActivity.this, SearchActivity.class);
            startActivity(intent);
            break;

        case R.id.btnSignout:
            mAuth.signOut();
            Intent signInIntent = new Intent(HomeActivity.this, MainActivity.class);
            startActivity(signInIntent);
            finish();
            break;