• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps What design pattern to use to reduce redundancy?

AlanFirblazer

Lurker
Nov 14, 2017
1
0
Hi!

I am writing an app. In one of the requirements, I have 2 activities which are almost identical in the content and layout. Almost 90% of them are identical. The rest of the 10% requires only changing the text labels and content which comes from a backend server.

Instead of have 2 activities and 2 layouts which are almost the same, I have 1 activity and 1 layout and based on a criteria, I have an "if else" statement to change those labels and content of the layout file in the activity.

To further reduce code redundancy and in future if I need to add more content to it, which design pattern should I use for this scenario?

I am reading up on the decorator pattern but if you could advise a more accurate pattern, I would be most thankful.
 
You shouldn't have different activities when the only difference is in data from the backend. Instead just update the values in your UI depending on the response from the backend.

If you want to build dynamic reusable (and especially testable) user interfaces you need to start using components to build your Activities. Otherwise they easily end up with way too much logic in them which make them extremely annoying to maintain. Fragments is one way to do it, it has a lot of issues though, I would recommend something like https://github.com/bluelinelabs/Conductor instead though.

But I'm getting ahead of myself, it seems you're just getting started so one thing at a time... If you don't use Retrofit yet that's #1 to learn.

Good luck!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones