如何在不违反DRY原则的情况下从多个活动中开始一项活动?

我正在开发一个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);

    }

}


隔江千里
浏览 169回答 3
3回答

30秒到达战场

我对DRY原理没有任何了解,但是在阅读此答案时,我得到了DRY原理背后的想法,而在考虑您的问题解决方案时,我得到了创建util类的想法,该类具有在所需活动之间进行导航的方法:public class NavigationUtils{public static void navigate(Context source, Class<?> destination){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(source, destination);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; source.startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; }}希望这个答案对您有帮助。

肥皂起泡泡

首先-不要崇拜DRY。它是一个经验法则,并非最终都是好的编程。其次,通常我要做的是class SignInIntent extends Activity {&nbsp; &nbsp; public static Intent createIntent(Context context, parameters) {&nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(context, SignInIntent.class);&nbsp; &nbsp; &nbsp; &nbsp; //Pass parameters here&nbsp; &nbsp; &nbsp; &nbsp; return intent;&nbsp; &nbsp; }}class SomeOtherActivity extends Activity {&nbsp; &nbsp; public void someFunc() {&nbsp; &nbsp; &nbsp; &nbsp; startActivity(SignInIntent.createIntent(this, params);&nbsp; &nbsp; }}通常,我希望它返回一个意图,而不是只是出于某些原因而等待他们开始活动。这样,您还可以在SignInActivity中保留所有需要的参数及其名称的所有知识。

蝴蝶不菲

好的考虑因素之一是使其具有BaseActivity通用功能。public class BaseActivity extends AppCompatActivity {&nbsp;public void startActivity(Class<?> mClass, Bundle extras){&nbsp; &nbsp; Intent intent = new Intent(this, mClass);&nbsp; &nbsp; intent.putExtras(extras);&nbsp; &nbsp; startActivity(intent);}public void startActivity(Class<?> mClass){&nbsp; &nbsp; Intent intent = new Intent(this, mClass);&nbsp; &nbsp; startActivity(intent);}}public class SignUpActivity extends BaseActivity {&nbsp; @Override&nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_sign_up);&nbsp; &nbsp; mSignInButton = findViewById(R.id.sign_in_button);&nbsp; &nbsp; mSignInButton.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(SignInActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java