1
\$\begingroup\$

Here is my case I made list of items when I click at any item it should navigate to certain activity.

Here is my I am done First I set number of constants to distinguish between each activity

// constants to show activities onclick event
private static final int LOC = 0, SERVICES = 1, PRE_APPROVE = 2, CLAIMS = 3, BENEFITS = 4, QUE = 5;

Secondly I create method navigate according to this constant

private void ShowActivity(int position) {
        switch (position) {
            case LOC:
                NavigateTo(LocLacatorActivity.class);
                break;
            case SERVICES:
                NavigateTo(ServicesActivity.class);
                break;
            case PRE_APPROVE:
                NavigateTo(PreApproveActivity.class);
                break;
            case CLAIMS:
                NavigateTo(ClaimsActivity.class);
                break;
            case BENEFITS:
                NavigateTo(BenefitsActivity.class);
                break;
            case QUE:
                NavigateTo(QuestionsActivity.class);
                break;
        }
    }


 private void NavigateTo(Class mActivity) {
        Intent intent = new Intent(MainActivity.this, mActivity);
        startActivity(intent);
    }

Finally , here is how I call the method

servicesButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     ShowActivity(SERVICES);
                }
            });

Is this good way to handle multi activity navigation, or can I decrease the size of my methods?

\$\endgroup\$
3
\$\begingroup\$
  1. What do you see as the advantage of your approach? Why not just:

    servicesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 showActivity(ServicesActivity.class);
            }
        });
    

    without the constants and the switch converting them to their respective classes? What value do you feel this additional abstraction layer adds to the code?

    (If you got rid of it, you'd only need showActivity implemented as follows:

    private void showActivity(Class mActivity) {
        Intent intent = new Intent(MainActivity.this, mActivity);
        startActivity(intent);
    }
    

    )

A few other remarks:

  1. By convention, names of methods in Java should start with lower-case (so, showActivity and navigateTo - just like startActivity and setOnClickListener).

  2. Parameter names shouldn't start with "m". There is a - controversial - naming convention that prefixes class members (or fields) with "m". I personally think it's iffy, but some people do that. This convention at least has some logic behind it though, whereas prefixing a parameter with "m" makes no sense.

  3. As of now you can pass any class to this method, which isn't as type safe as it could be. If you made it generic, you could narrow it down to Activity subclasses, making it more typesafe:

    private <T extends Activity> void showActivity(Class<T> activity) {
        Intent intent = new Intent(MainActivity.this, activity);
        startActivity(intent);
    }
    

    Now you can't call eg. showActivity(Integer.class).

  4. Other than removing the "m" prefix from the parameter name, I wouldn't also keep it named "activity". It's an Activity class, not an Activity (instance object) itself. I'd rename it to activityClass.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.