Skip to main content

Posts

Showing posts with the label Fragment

Fragment LifeCycle

Normal setRetainInstance(true) Description onAttach onAttach Called once the fragment is associated with its activity. onCreate - Called to do initial creation of the fragment. onCreateView onCreateView Creates and returns the view hierarchy associated with the fragment. onActivityCreated onActivityCreated Tells the fragment that its activity has completed its own Activity.onCreate(). onViewStateRestored onViewStateRestored Tells the fragment that all of the saved state of its view hierarchy has been restored. onStart onStart Makes the fragment visible to the user (based on its containing activity being started). onResume onResume Makes the fragment begin interacting with the user (based on its containing activity being resumed). As a fragment is no longer being used, it goes through a reverse series...

Retain AsyncTask when configuration change

One of the frequent facing situation in Android is how to handle configuration change such as device rotation, soft keyboard and so on. For example, what happens if you rotate the device when you download a file using AsyncTask? The Android would create your Activity again to load the proper configuration including layout. The lifecycle methods(onCreate - onStart - onResume) will be called again and you can get data onCreate() if you have saved any data onSavedInstance(). However, your AsyncTask lost pipeline to your Activity and the code of executing the AsyncTask will be called again. This is not definitely what we want. Let's have a look at code how to avoid it: I'm going to create a simple app to display weather using Open Weather API for demo. We need one Activity, one Fragment for UI ListView first. MainActivity.java import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bun...

Google's fragment example error

These days, I have been reading through Android Developer training document(http://developer.android.com/training/index.html) in detail and playing with samples. I am in the Fragment section now and I found out an example app has an error. Here is the link: http://developer.android.com/shareables/training/FragmentBasics.zip I give you a brief about the example. Entry point of the app is MainActivity and it uses different layouts depending on the screen sizes layout, one for normal and the other for large size devices. You can see only news article list when you run the app on handset devices but it will display two pane layout on large size device such as 7" tablets. /res/layout/news_article.xml(Normal size) /res/layout-large/new_article.xml(Large size) It will load news content when you click a item from the headlines. The MainActivity switches Fragment to display it in a handset but it displays news content in the second pane in a table device. Error c...