23

In my application, I have tabbar functionality. In one tab i am displaying server data in lisview, and on clicking on that detail page for that list item will be open in new fragment.

But when I press back button from that detail page, every time oncreateview called of previous page, so every time listview created and new fetches new server data. So how to prevent such and just display previous state when back button press?

Mihir Shah
  • 1,799
  • 3
  • 29
  • 49

3 Answers3

45

I know it has been too long to give this answer but what i am guessing is you are replacing your fragment with other one. I mean to say is you are using

ft.replace(R.id.realTabContent, fragment);

to move to other fragment which is you are using in your onItemClick so simple solution is use

ft.add(R.id.realTabContent, fragment);

instead of replacing your fragment.

Understand the difference between replace and add. This will solve your problem.

Replace : it will replace the original fragment and re-create the view when you come back
Add : it will just add a new fragment to stack.

Hope this will help someone who is facing the same problem...

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
  • 3
    if we set `Add` then new fragment overlap the old one – Vasile Doe Oct 26 '17 at 12:15
  • 1
    This isn't correct: the docs say that *add* will add this fragment to the container, not add it to the back stack. https://developer.android.com/reference/android/app/FragmentTransaction.html#add(int,%20android.app.Fragment,%20java.lang.String) – liucheia Mar 09 '18 at 23:54
  • 1
    For me when i tried to add new fragment in same container, screen still shows old fragment data. In my activity i have container, in that i am adding Fragment1 and when user click on "continue" in Fragment1, it need to show details page, for this if i try to add new Fragment in same container, i could see that onAttach and onCreateView of Fragment2 called, but UI still shows Fragment1. **any solution for this?**. I am using android.support.v4.app.Fragment – Ashok Reddy Narra Jun 15 '19 at 18:47
  • In order to prevent overlapping you might just set `android:background="@color/white"` in the root view of your second fragment to make it not transparent. – Taha Dec 10 '21 at 04:25
6

I don't think prevent calling onCreateView is a good idea, beside if the function is not called, there will be exception, so NO, you shouldn't. Instead of doing that, I suggest you move your "listview created and new fetches new server data" into another place where you can call explicitly (when you want, where you want, onCreate() is a good place), don't put it in onCreateView(). Hope this helps.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
user2652394
  • 1,686
  • 1
  • 13
  • 15
-1

You should cache your data objects apart from view variables as their references needs to be updated if you are fetching any data from server and don't want to make a call again then use branching to branch out that code.

Create an init() method where you do all initialization and server calls and logically branch out call to init() method whenever you don't want to call init().

Sumit Dhaniya
  • 596
  • 6
  • 14