W.K.S
W.K.S

Reputation: 10095

Code Repetition Confusion

In my Android Application, I have two activities.

The way I've implemented is this:

The problem is that the two activities pretty much do the exact same thing on preExecute, postExecute and progressUpdate so there's code repetition between the two activities.

How can I fix this?

Upvotes: 0

Views: 137

Answers (2)

Moog
Moog

Reputation: 10193

An alternate to Catherine's suggestion is to create an activity mode enumeration.

Pass this mode as an extra when launching your activity.

If the mode is MODE_GALLERY then load the gallery.xml layout and populate it, if not then load the other layout.

Just make sure that you use the same id's for the common views, an easy way to do this is to use the include tag in your layout files.

The advantage of this is that you only have one activity file instead of three which would be required for the subclassing method.

You may also be able use fragments, but I don't have any experience with these so I can't advise ou further.

One last note, I would avoid putting UI code into a task.

Upvotes: 1

Catherine
Catherine

Reputation: 14010

Implement a base class for the two activities that executes common code. Implement the activities as subclasses of your base class to execute different code.

Upvotes: 3

Related Questions