I'm building an Android app whose navigation is similar to Instagram, which has multiple tabs and each tab has its own history.
I implemented this navigation by using ViewPager
with FragmentPagerAdapter
. When navigating down each tab, I replace the FrameLayout
's content by executing following code
getChildFragmentManager.beginTransaction().addToBackStack(null).replace(R.id.container, newFragment).commit()
and execute getChildFragmentManager.popBackStackImmediate()
for back button press event.
I use facebook fresco's SimpleDraweeView
and load image by executing SimpleDraweeView.setImageURI(Uri.parse(str))
.
The application uses MapView
and creates custom markers, which has icon that is created by Picasso.with(context).with(url).into(new MarkerTarget(...))
class MarkerTarget implements Target {
private Marker marker;
public MarkerTarget(Marker marker) {
this.marker = marker;
}
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
try {
marker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
marker.setVisible(true);
} catch(Exception e) {
}
}
...
}
}
Most of the fragments have ListView
, an extenstion of BaseAdapter
, and a presenter.
It seemed to work but the problem is that when I navigate down in a tab, OOM Error occurs. How does Instagram keeps history of (possibly) infinite numbers.
Allocation Tracker Result (Screenshot)
How can I implement instagram like navigation on Android platform ?
There are multiple ways to do this:
The OOM error could happen because of a bunch of reasons. 1. As fragments pile up, you are holding onto some data per fragment. You might want to save state and restore the data to save memory. 2. Using a page adapter could cause this as a page adapter can keep fragments alive even if they are not currently visible.
Try running you app and studying the HPROF files for clues to memory usage.