-3

I am a beginner developer of an Android app and have a structure related question.

In my app I have a few screens with buttons that allow us to switch between these screens.

Right now I am setting up the SAME chunk of code with the button click listeners in each of these screen java file. I feel this is bulky and repetitive.

Is there a way to reference this chunk of code from a SEPARATE java file instead of duplicating it in each screen?

What direction should I look into? I feel it has something to do with inheritance of classes, but could you give an expert view on that?

Thank you so much! Anne

ADDED FROM HERE: LETS SAY I have the following code:

 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_dialog);

  ImageButton goCreateBut = findViewById(R.id.createIcon);
  goCreateBut.setOnClickListener(new View.OnClickListener() {

     public void onClick(View v) {
          startActivity(new Intent(ListenRepeat.this, Recreate.class));
         }
  }

And this piece of code is repeated in each of 4 Activities. They all referencing the same button (in fact i have 4 more of similar buttons repeated this way). Is there anyway to cut on the repetitiveness? Thank you!

anne
  • 29
  • 5
  • Post the relevant part of your code here to we can help. It's hard to propose a solution if we can't actually understand what's going on. – Talendar Sep 22 '18 at 14:54

1 Answers1

0

It is hard giving you any concrete help without knowing your code. But generally: Good programmers are lazy and lazy people don't repeate. To get rid of duplicates, try to make methods out of your code and call them with different parameters to get different results by the same piece of code. You can also inherit from other classes

Krissini
  • 23
  • 11