Best way to switch between two fragments

GLee

I'm interested in the best way to have a single activity that switches between two fragments.

I've read probably 15 Stack Overflow posts and 5 blogs posts on how to do this, and, while I think I cobbled together a solution, I'm not convinced it's the best one. So, I want to hear people's opinions on the right way to handle this, especially with regards to the lifecycle of the parent activity and the fragments.

Here is the situation in detail:

  1. A parent activity that can display one of two possible fragments.
  2. The two fragments have state that I would like to persist across a session, but does not necessarily need to be persisted between sessions.
  3. A number of other activities, such that the parent activity and the fragments could get buried in the back stack and destroyed due to low memory.
  4. I want the ability to use the back button to move between the fragments (So as I understand it, I can't use setRetainInstance).

In addition to general architecture advice, I have the following outstanding questions:

  1. If the parent activity is destroyed due to low memory, how do I guarantee that the states of both fragments will be retained, as per this post: When a Fragment is replaced and put in the back stack (or removed) does it stay in memory?. Do I just need a pointer to each fragment in the parent activity?
  2. What is the best way for the parent activity to keep track of which fragment it is currently displaying?

Thanks in advance!

GLee

I ended up adding both of the fragments using the support fragment manager and then using detach/attach to switch between them. I was able to use commitAllowingStateLoss() because I retain the state of the view elsewhere, and manually set the correct fragment in onResume().

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.my_layout, new AFragment(), TAG_A);
    fragmentTransaction.add(R.id.my_layout, new BFragment(), TAG_B);
    fragmentTransaction.commit();
}

public void onResume() {
    super.onResume();
    if (this.shouldShowA) {
        switchToA();
    } else {
        switchToB();
    }
}

private void switchToA() {
    AFragment fragA = (AFragment) getSupportFragmentManager().findFragmentByTag(TAG_A);
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.detach(getSupportFragmentManager().findFragmentByTag(TAG_B));
    fragmentTransaction.attach(fragA);
    fragmentTransaction.addToBackStack(null);

    fragmentTransaction.commitAllowingStateLoss();
    getSupportFragmentManager().executePendingTransactions();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the best way to switch between projects in Vim?

From Dev

Best way for replacing fragments

From Dev

Android fragments switch best practices

From Dev

How can I switch between two fragments, without recreating the fragments each time?

From Dev

What is the best way to communicate between two servers?

From Dev

best way to fade between two bitmaps in android

From Dev

Best way to transmit data between two applications

From Dev

Best way to describe - between two numbers

From Dev

onItemClickListener between two fragments

From Dev

Communicate between two fragments

From Dev

Communication between two fragments

From Dev

switch between fragments without 'FragmentPagerAdapter'

From Dev

Best way to switch values

From Dev

Using viewer page to switch between two fragments - however it disappears after switching into a fragment

From Dev

Proper way of communication between fragments

From Dev

Adding a listener between two fragments

From Dev

Pass data between two fragments

From Dev

Common way to switch fragments without loosing state

From Dev

What is a proper way to switch between two views of json?

From Dev

How to switch between fragments without actually overlapping the previous fragments

From Dev

How to switch between fragments during onclick?

From Dev

Using viewpager with radiogroup to switch between fragments

From Dev

Reload Activity to re-switch between fragments

From Dev

Reload Activity to re-switch between fragments

From Dev

Best way to handle android fragments backstack

From Dev

Best way to go back and forth between two activities?

From Dev

Is this the best way to convert between two different types of array?

From Dev

What is the best way to store and use information between two objects in python?

From Dev

ng-map show the best way between two points (waypoints)

Related Related

  1. 1

    What is the best way to switch between projects in Vim?

  2. 2

    Best way for replacing fragments

  3. 3

    Android fragments switch best practices

  4. 4

    How can I switch between two fragments, without recreating the fragments each time?

  5. 5

    What is the best way to communicate between two servers?

  6. 6

    best way to fade between two bitmaps in android

  7. 7

    Best way to transmit data between two applications

  8. 8

    Best way to describe - between two numbers

  9. 9

    onItemClickListener between two fragments

  10. 10

    Communicate between two fragments

  11. 11

    Communication between two fragments

  12. 12

    switch between fragments without 'FragmentPagerAdapter'

  13. 13

    Best way to switch values

  14. 14

    Using viewer page to switch between two fragments - however it disappears after switching into a fragment

  15. 15

    Proper way of communication between fragments

  16. 16

    Adding a listener between two fragments

  17. 17

    Pass data between two fragments

  18. 18

    Common way to switch fragments without loosing state

  19. 19

    What is a proper way to switch between two views of json?

  20. 20

    How to switch between fragments without actually overlapping the previous fragments

  21. 21

    How to switch between fragments during onclick?

  22. 22

    Using viewpager with radiogroup to switch between fragments

  23. 23

    Reload Activity to re-switch between fragments

  24. 24

    Reload Activity to re-switch between fragments

  25. 25

    Best way to handle android fragments backstack

  26. 26

    Best way to go back and forth between two activities?

  27. 27

    Is this the best way to convert between two different types of array?

  28. 28

    What is the best way to store and use information between two objects in python?

  29. 29

    ng-map show the best way between two points (waypoints)

HotTag

Archive