5

There are two things which I like on the Instagram for Android app and I'd like to implement them in my app.

1. Infinite go back in history of fragments

If you tap on a user, you can see his details, taping on followers will return a list of followers, pressing on another user will show his details... and so on. Basically you can do this thing for many times BUT when you go back everything is instant without loading. How can this be implemented? My initial thought was to have only one activity with a top actionbar and for the rest use fragments (one fragment for user details, one fragment for users list) and so on. The problem is I can't think of a good way to allow going back in history. The only way I can see is by caching all the data (user data / list adapters) is an ArrayList so when the user presses back, take the last item from the list and instantiate the fragment. Is there a better way of doing it ? I'm thinking I could start a new activity for each user interaction and them when the user presses back, simply finish the current one. My only worry in this case would be running out of memory. Is there a way to cache fragments with their state ?

2.GridView inside ScrollView

On user details there are two main layouts: a layout with user details and a gridview of images. When the user scrolls, at the scroll's end, the gridview gets new set of items (load as you scroll). While I know how to implement load as I scroll for the gridview, I don't know how to add the gridview inside a scrollview and keep listening for scroll events

enter image description here

Alin
  • 14,809
  • 40
  • 129
  • 218
  • For first thing after back pressed, Data not loading as per requirenment.. While you calling the function for fetch data from server you have to make a static adapter for fragment in that activity from which fragment being called...And Check that if adapter==null then fetch data from server otherwise you can directly set adapter to listView or GridView. – Piyush Sep 03 '13 at 08:49
  • For the first part, Fragments can do `saveInstanceState()`, and get it back in `onCreate()`. Add a disk cache between data request and response, and you've got your old data saved for quick re-use when old fragments come to stack top. For the second part, it seems they have extended the `GridView` and added a header functionality. – S.D. Sep 03 '13 at 08:49
  • For one, use `addToBackStack()` but you'll need to save the data yourself. For two, you could make your own `GridView` to have a header or you could make a `ListView`(with the top data being a header) look like a `GridView`(you could see a small example here https://github.com/luksprog/DroidPlayground/blob/master/src/com/luksprog/playground/adapter/ExpandingRowItemDetails.java). – user Sep 03 '13 at 11:44
  • @Alin Did you get the solution for second question? – Noundla Sandeep Dec 17 '13 at 09:21
  • @noundla in the end I just had to use listview and inflate one layout for the header and another layout for the rest of the items. – Alin Dec 17 '13 at 11:01
  • @Alin Thanks, I have done the same. I dint get any good solution than that. – Noundla Sandeep Dec 18 '13 at 13:21

1 Answers1

0

Haven't got a quick answer for number 2 but for the first question why not just add the fragments to the backstack with FragmentTransaction.addToBackStack ?

That way you get the natural back-action with fragments without having to start new activities for every action.

Liminal
  • 1,709
  • 2
  • 12
  • 15
  • I haven't used addToBackStack before. If I add a fragment to back stack will it store all the fragment details (like a cached version of it) ? I mean will it keep all variables, images, views and so on ? – Alin Sep 03 '13 at 08:54
  • Alin ,for saving the state you have to first save the instance and when you back to the layout you have restore it , it's not done automatically , for eg when I tried addtobackstack my recyclerview just goest to starting posting and not able to continue where I left it – Vasant Raval Jul 11 '21 at 07:01