Home » Android » android – How to handle multiple Fragments for a single Activity

android – How to handle multiple Fragments for a single Activity

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have recently started to take a look at developing apps for my android device. What started this interest for me is I was playing around with a few arduinos had the great idea to make them communicate with my phone, as say an interface for whatever values I am measuring on the arduino itself. Now I could take the easy way out and use a public source to accomplish this but there isn’t as much to learn that way, and I would like it the way I want it.

Now I suppose the first question I need to ask is, would multiple fragments/single activity be the best way for me to accomplish this? Basically, I want 1 connection to the arduino, pull all the values, but depending on the “tab” I have selected I want certain values displayed certain ways. I decided to make each tab a different fragment and just display the values different ways. Like I said, I am just beginning android development experience so don’t have much to base this choice off of.

So being fixated on this multiple fragment idea I have:

  1. Created multiple Fragment.xml files
  2. Defined a class for each separate view
  3. Created a List to display available Fragments
  4. Instantiated and displayed fragment when selected

So essentially my onMenuItemSelect looked like this.

FragmentTransaction FT = getFragmentManager.beginTransaction();
switch(position){
    case 1:
        FT.replace(R.id.fragment_container, new MyFragment()).commit();
        break;
    case 2:
        FT.replace(R.id.fragment_container, new MySecondFragment()).commit();
        break;
}

The above code worked, it did what I wanted it to without any issues. I don’t really like this though, because for each and every Fragment I wanted to add I would need to add a new case to the switch. Also this instantiates a new fragment every time, even if one was already created. Is that a problem?

The biggest problem I had with it is that it isn’t the easiest to scale. For 2-3 fragments this isn’t the worst way to handle it (in my eyes). I want to be able to have as many fragments I want without an individual case for each one in the switch. So what I did was created a fragmentList to hold a single instance of each of my fragment.

List<Fragment> fragmentList;

private void populateFragmentList();{
    fragmentList = new ArrayList<Fragment>();
    fragmentList.add(new HomeFrag());
    fragmentList.add(new BluetoothFragment());
    fragmentList.add(new USBFragment());
    fragmentList.add(new RCInfoFragment());
    fragmentList.add(new ControllerFragment());
    fragmentList.add(new FingerCordsFrag());
}
public void onMenuItemSelect(int position, int curPosition){
    if(fragmentList.get(position).isAdded()){
       getFragmentManager().beginTransaction().show(fragmentList.get(postition))
        .hide(fragmentList.get(curPosition)).commit();
    }
    else
        getFragmentManager().beginTransaction().add(R.id.fragment_container, fragmentList.get(position)).show(fragmentList.get(position)).hide(fragmentList.get(curPosition)).commit();
}

And this method also worked. I could get it to display all of my fragments, without have to re-instantiate each fragment each time. I believe this does what I am wanting it to do, it scales fairly well(better than a switch/case IMO). The problem that I have now is that it all goes crazy when I change orientation. Up until now I was only testing portrait mode, I am not able to view any of my fragments when I select them in other orientation. I can start it in either orientation, and it works, but when I change it during run-time, I am only able to see the one fragment I had open when I changed orientation.

Now, each fragments “onCreateView” is being called, it is just that the display isn’t being shown. I believe I have it narrowed down to it isn’t being attach to the new activity created from the orientation change. Is There anyway I can reattach fragments that are already created to a new activity.

In summary, the questions I have are:

  1. Is this model even a good way for my application?
  2. Is there a decent way to handle Fragments that scales well? Can’t
    seem to find any examples.
  3. Is using a ”new MyFragment()” each time I open a different tab a
    reasonable way to achieve this?
  4. Is my way of storing my Fragments in a list a reasonable way to
    handle them?
  5. How do I reattach a fragment to the new Activity after an
    orientation change?

Thank you for your time.

*Had to type all this code on the fly because I, for some reason, couldn’t get my C/P’d code to format correctly.

How to&Answers:

I believe it a good choice to use fragments and start with this example

You should definitely override some “Adapter” to handle all the transactions more easily…

Check here for the orientation problem…