5

I inherited some code at work and I have a question about some implementation. The application I'm working on has an Activity that contains about 15 different fragments. The logic in the Activity handling these fragments can roughly be summarized with the following pseudocode:

if (button_1 selected) { 
    load fragment_1; 
} else if (button_2 selected) {
    load fragment_2;
} else if (button_3 selected) {
    load fragment_3;
} ...and so on x15ish

My question is: does there exist some kind of Android design pattern to handle situations like this? The code works; however, I don't feel too comfortable with a giant if/else or case statement. I saw this question and it seems very similar to the problem that I'm having. I did quite a bit of searching on the internet but I haven't found examples or best practices for this kind of scenario.

If someone can point me in the right direction or have some suggestions; that'd be awesome. Thanks!

Community
  • 1
  • 1
Dung Ta
  • 789
  • 5
  • 17
  • have you checked this http://developer.android.com/intl/zh-cn/training/basics/fragments/communicating.html ? – Khizar Hayat Apr 26 '16 at 05:51
  • You usually dont have a if-else chain, usually you use tabs to change fragments or onClick methods for each fragment – Nanoc Apr 26 '16 at 06:03
  • @KhizarHayat, yeah I've done my fair share of of reading on fragments and I've used it in quite a few projects. However, in this scenario, I've never actually seen this number of fragments contained in a single activity and implemented in the if/else statement. – Dung Ta Apr 26 '16 at 06:21
  • @Nanoc, for simplicity's sake i pseudocoded the implementation; but there is an onClick method for each button that leads to the if/else chain to determine which fragment to load. One extra detail that I should have mentioned is that each fragment is full screen and it replaces the container FrameLayout in the container Activity. – Dung Ta Apr 26 '16 at 06:26
  • Then you can avoid the if/else chain if you have a button for each fragment, also fragments wasnt meant to be fullscreen ( someone say they take a lot less memory anyway). You cant avoid having to say when X button clicked go to screen Y... – Nanoc Apr 26 '16 at 07:12
  • 1
    Have you decided on how to resolve your issue? – PsiX Jul 14 '16 at 16:29

2 Answers2

0

You should not check which button has been selected but rather use the button's onClickListener to select the correct fragment.

buttonForFragment1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
             // select fragment 1 here
    }
});

In regards to the question, this is not a level of design patterns but rather implementation details (idioms) and you correctly recognized your code as a smell and I think one possible solution which does not qualify as a pattern is the code above.

PsiX
  • 1,557
  • 1
  • 17
  • 33
0

For each button in your layout you can assign a method in your activity:

<Button 
    ...
    android:onClick="startFragmentOne" />

Then implement those methods:

public void startFragmentOne(View view) {
    //TODO
}
dev.bmax
  • 7,278
  • 2
  • 27
  • 38