Our tutorial app has one activity and two fragments. One fragment contains a list and the other an image.
Running the app on a small device in portrait mode, displays the list fragment. Selecting an item in the list displays the second fragment containing an image.
Flipping the device to landscape displays both fragments side by side.
Running the app on a tablet displays the fragments side by side in both portrait and landscape mode.
Think of components like buttons and images. You can reuse them over and over in different activities. You can also rearrange them within an activity.
Fragments are similar.
Fragments let you divide your activity into reusable components which have their own User Interface and lifecycle.
The data that is saved by the system to restore the previous state of an activity is called the Instance State. This data is saved as a collection of key/value pairs in a Bundle object.
When an activity is paused or stopped, its state is kept in memory. When the activity resumes, it is restored from memory.
When the activity is destroyed, it loses all information about its state. When the activity is restarted, it is created from scratch. It is a new activity.
Each app runs in its own process and contains one or more activities.
The Android run time manages these processes and decides which app to kill if it needs resources.
An app’s priority helps determine if the run time will kill it and its running activities.
The app’s priority is influenced by its highest priority activity.
If the Android’s memory manager needs to free up memory, it looks at the activity stack to determine the priority of the activities and which ones can be closed.
The download manager was introduced in Android 2.3 (API level 9).
It’s a System Service that optimises the handling of long-running downloads in the background.
The download manager handles HTTP connections, monitors connectivity changes, reboots, and ensures each download completes successfully
Its good practice to use the download manager for most downloads, especially if the download continues between user sessions or if success is important.
All Android components (like activities and services) start on the main thread. This is the thread that the user interacts with.
You need to keep this thread clear so that the app does not hang. It’s a good idea to put all time-consuming processes on a separate thread.
Put file operations, network lookups, database interaction, etc. on a background thread.
You should do time consuming work, like downloading files, on a separate thread.
This will free up the main thread so your user can carry on using the app.
IntentServices are the preferred way for doing long running background operations.
You can request an IntentService from any of your fragments or activities. The requests are queued and handled one-after-the-other. The IntentService stops once all the requests have been handled.
Sometimes, you may need to get data from another activity. That activity could be in your app or in another app.
You could use startActivityForResult() to get that data for you.
You may want to pass data from one activity to another.
You could put the data in a central point and then access it from anywhere.
Here are some of the ways to do that: