1

I have been working on application which has 3 types of accounts related to it. We create a single layout and view/hide items on it depending on, from which account you are currently logged in.

With this approach, we have activities/fragments doing a lot of different things, they handle all cases wrapped in if/else checks etc. With growing project, it is becoming hard to maintain these classes.

Say, if I have to hide a view in certain scenario, I have to look around many if/else checks just to hide a single button because if I hide it on one place other check will make it visible again, really hard to maintain.

I am looking forward for best advises on this issue from the experts.

mallaudin
  • 4,744
  • 3
  • 36
  • 68

5 Answers5

1

If your separate apps require minor customization and theme changes, but are really the same base app, multiple flavors is definitely the way to go. However, if both apps require a lot of custom code differences, you might want to rethink using multiple flavors strategy.

Also, take notice of the difference between flavors and build types. Use flavors for situations where you might need different versions of the same app in the Play Store, for example, free and pro, or for situations where you are customizing the same app for multiple clients.

for details http://www.androidauthority.com/building-multiple-flavors-android-app-706436

Pavya
  • 6,015
  • 4
  • 29
  • 42
1

If you are struggling with a lot of if/else scattered in the code, maybe you should use polymorphism in your code.

Create an abstract class for the Activity, then specialize it for each particular type.

Use the Factory method pattern for creating objects of this hierarchy. This method will use the parameters for deciding which concrete class to instantiate, and then it will initialize the instance being returned.

Use the Template Method pattern if there is an algorithm common to all sub-classes but that contains some open steps that should be implemented by each class.

Use the State/Strategy pattern if you need polymorphic code that may be modified at runtime.

0

Create different xml for same layout and use <include layout="@"/> Tag to create the layout, it will reduce if/else and also provide you the code re-usability

KKSINGLA
  • 1,284
  • 2
  • 10
  • 22
0

you have create new xml files in which has common view's for your activity and fragment then need to use include tag in xml for adding those common view's into your activities & fragments xml.

vivek mahajan
  • 521
  • 3
  • 16
0

I think you should create separate layout for all 3 types of account and you can create PickLayout static class/method to pick the layout by type

int getLayout(int type){ return layoutMap.get(type); }

if you have re-usable layout then you should use include, merge or you can use ViewStub also.

if you have chain of if/else then you should use Map link that will be scale-able, error-prone free.

And try to follow android suggested design-pattern that will be helpful for writing test case also.

Community
  • 1
  • 1
dastan
  • 892
  • 10
  • 17