Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Matheus G Oliveira
Matheus G Oliveira
9,682 Points

Refactoring tips please

Hey people

So, my app has 4 layout xml files and 32 java activities. I don´t think that is normal (to have 32 java activities) so I would like to make it with less java files. Correct me if im wrong

Basically they do the same.

i have a MainActivity that has 4 buttons and they bring you to Activity1, Activity2, Activity3 or Activity4.

From the Activity1, i have another 5 buttons that bring you to other activities.

Lets say

Activity1 >Activity11 >Activity12 >Activity13 >Activity14 >Activity15

This is my OnClick from Main Activity:

   class btnHandler implements OnClickListener{

               @Override
        public void onClick(View v){

                      if(v.getId()==R.id.btnMain1){

      Intent i = new Intent();
        i.setClassName("om.example.refactoringtest",    "com.example.refactoringtest.Activity1");
        startActivity(i);

    }else if(v.getId()==R.id.btnMain2){
        Intent i = new Intent();
        i.setClassName("om.example.refactoringtest", "com.example.refactoringtest.Activity22");
        startActivity(i);

    }else if(v.getId()==R.id.btnMain3{
        Intent i = new Intent();
        i.setClassName("com.example.refactoringtest",    "com.example.refactoringtest.Activity3");
        startActivity(i);

    }else if(v.getId()==R.id.btnMain4){
        Intent i = new Intent();
        i.setClassName("om.example.refactoringtest",   "com.example.refactoringtest.Activity4");
        startActivity(i);

    }

                    }           

    }

And this is my OnClick from the Activity1:

  class btnHandler2 implements OnClickListener{

  @Override
        public void onClick(View v){

       if(v.getId()==R.id.btn1) {
   Intent i = new Intent();
        i.setClassName("om.example.refactoringtest",     "com.example.refactoringtest.Activity11");
        startActivity(i);

  }else if(v.getId()==R.id.btn2){
            Intent i = new Intent();
            i.setClassName("om.example.refactoringtest",   "com.example.refactoringtest.ActivityAnswer12");
            startActivity(i);

            }else if(v.getId()==R.id.btn3){
      Intent i = new Intent();
        i.setClassName("om.example.refactoringtest", "com.example.refactoringtest.ActivityAnswer13");
        startActivity(i);

    }else if(v.getId()==R.id.btn4){
        Intent i = new Intent();
        i.setClassName("om.example.refactoringtest", "com.example.refactoringtest.ActivityAnswer14");
        startActivity(i);

    }else if(v.getId()==R.id.btn5){
        Intent i = new Intent();
        i.setClassName("om.example.refactoringtest", "com.example.refactoringtest.ActivityAnswer15");
        startActivity(i);

    }
    }           
      }
  };

Does anyone know some way to refactor these to make it like, compact?

Appreciate the help!