我正在开发一个Android应用程序,其中某些活动可能是从多个活动开始的,我知道如何使用Intent在活动之间进行切换。我只是在不违反DRY(请勿重做)原则的情况下努力编写代码。
下面显示了该代码,其中重复了从两个不同的活动启动同一活动的代码:
MainActivity.java启动SignInActivity.java
public class MainActivity extends AppCompatActivity {
private Button mSignUpButton;
private Button mSignInButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme_MainActivity);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSignUpButton = findViewById(R.id.sign_up_button);
mSignInButton = findViewById(R.id.sign_in_button);
mSignUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSignUp();
}
});
mSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSignIn();
}
});
}
private void startSignUp() {
Intent signUpIntent = new Intent(this, SignUpActivity.class);
startActivity(signUpIntent);
}
private void startSignIn() {
Intent signInIntent = new Intent(this, SignInActivity.class);
startActivity(signInIntent);
}
}
SignUpActivity.java启动SignInActivity.java
public class SignUpActivity extends AppCompatActivity {
private Button mSignInButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
mSignInButton = findViewById(R.id.sign_in_button);
mSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSignIn();
}
});
}
private void startSignIn() {
Intent signInIntent = new Intent(this, SignInActivity.class);
startActivity(signInIntent);
}
}
30秒到达战场
肥皂起泡泡
蝴蝶不菲
相关分类