1

I have an activity with 3 tabs. Tab A, Tab B, Tab C. Tab A loads data from a server. Tab B and C have text fields on them with buttons. Each tab is implemented with a different fragment.

My issue is when I type on a text field in Tab B and swipe to Tab C, the text field from Tab B is not cleared. I would like for it to be cleared. Is there a way to recreate or reload the fragment when I swipe to the selected tab?

For Tab A to finish loading data from the server takes some time, so I created a progress dialog while the operation is running. Now the progress dialog works correctly, but if I swipe from Tab A to Tab B to Tab C back to Tab B, the progress dialog is shown. The progress dialog is shown in the onCreateView() method of the fragment for Tab A. I don't understand how this is possible. I think it is because the fragments are stilling running even when I am on different tabs. Is it possible for the fragments to only run/start when the user swipes to the selected tab? Also, is there any code that I should run in the onTabUnSelected() method? Is there a way to end a fragment in that method?

Edit:

I figured out how to have the text fields cleared when I leave the fragments. I overrided the following: public class MyFragment extends Fragment

@Override
public void setMenuVisibility(final boolean visible) {
    super.setMenuVisibility(visible);
    if (!visible) {
        ...
    }
}

Now the text fields are cleared when I swipe from tab to tab. I'm not sure if this is the best way to solve the problem, so if somebody else has a better way, please let me know. I tried to implement the same function in the other fragment to dismiss the dialog when the fragment is not visible, but it does not work.

Edit 2:

For some reason, the onCreateView() method for fragment A is being run when swiping from Tab C to Tab B. Fragment A is loaded on Tab A. Shouldn't the onCreateView() method for fragment A only be run when Tab A is selected?

Edit 3:

I figured out what the problem. Android automatically sets the value of setOffscreenPageLimit(int) to setOffscreenPageLimit(1). This automatically loads the next tab in the background. So as a workaround, I will create a progress bar/spinner on the screen itself instead of creating a progress dialog.

OGH
  • 540
  • 1
  • 4
  • 15

1 Answers1

0

I will try answer your queries one by one.

  1. See this link and this link to learn how to communicate between fragments. i found second link (vogella) more useful practice.
  2. its is absolutely fine that on tab A is working in background on swiping to Tab B and C. You need to understand that your all 3 tabs belongs to one activity that may contain three different fragments.
Omama
  • 147
  • 1
  • 2
  • 9