Introduction
In Android app development: Fragment's non-disruptive save Setretaineinstance article has described how to get fragment to save data without destroying it with the activity. In the design of mobile application architecture, interface and data are inseparable and can not be confused. In most of the development experience, we are using fragment to interface programming, even if the data is basically only the interface related control data, rarely do other data preservation, after all, this and the development of the principle of the contrary, and today this blog will introduce the fragment of the alternative usage, Just to save the data without any interface elements.
Implementing the background
For fragment data preservation method, it is not difficult to think of or with setretaininstance relationship. This is where the background is used when screen rotation or other configuration changes are needed. Whether in development our interface is generated by activity or fragment, when the screen rotates, the control state and the necessary data are cached in the life cycle onsaveinstancestate. Typically, bundles are used to store data. As the official description of the bundle says, the bundle is a map used to store string and other serialized data types. Similarly, there is an exception in Android: http://developer.android.com/intl/zh-cn/reference/android/os/TransactionTooLargeException.html
This exception is literally not difficult to understand, is the transmission of data too large anomalies. As described in the description, the current Android system for the application of the transmission data size limit within 1MB. So it is not very safe to use bundles to cache big data during screen rotation. Such big data in Android is one of the classic Representative is bitmap, even if bitmap is already serialized data, can easily use the bundle as a cache medium, but I strongly do not recommend this. Below, it provides a simple solution.
Implementation process
First, create a fragment to hold the data:
public class Bitmapdatafragment extends Fragment {public static final String TAG = "Bitmapsaver"; Private Bitmap Bitmap; Private Bitmapdatafragment (Bitmap Bitmap) { this.bitmap = Bitmap; } public static bitmapdatafragment newinstance (Bitmap Bitmap) { return new bitmapdatafragment (BITMAP); } @Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setretaininstance (True); } Public Bitmap GetData () { return Bitmap; }}
This fragment has no interface and uses Setretaininstance (true) during the OnCreate lifecycle to ensure that the data is not destroyed with the vector.
Once created, practice using the process, assuming the user is activity:
@Override protected void onsaveinstancestate (Bundle outstate) { if (mbitmap! = null) { Getsupportfragmentmanager (). BeginTransaction () . Add (Bitmapdatafragment.newinstance (Mbitmap), Bitmapdatafragment.tag) . commit (); Outstate.putboolean (Sense_image_key, True); } else { Outstate.putboolean (Sense_image_key, false); } Super.onsaveinstancestate (outstate); }
When the device rotates, detects one of the bitmap displayed in the current interface, and if it does, new creates a fragment that we just created to put bitmap data in, Then add this fragment to the Fragmentmanager and specify tag so that we can find it conveniently after the state is restored.
At the time of recovery, the activity's life cycle goes to OnCreate (), where we can determine if there are bitmap data to be obtained by detecting the bundle parameters:
if (Savedinstancestate.getboolean (Sense_image_key)) { bitmapdatafragment fragment = (bitmapdatafragment) Getsupportfragmentmanager () . Findfragmentbytag (Bitmapdatafragment.tag); Bitmap = Fragment.getdata (); Getsupportfragmentmanager (). BeginTransaction (). Remove (fragment). commit ();}
PS: After removing the bitmap data we need, do not forget to remove the fragment as a data container from the Fragmentmanager, freeing up the system memory it occupies.
Summarize
It's quite simple to fragment non-mainstream usage, but it's really a bit more complicated than just using bundles to save data, but a more secure data transfer is a good thing for applications. Recommended index five stars ★★★★★!