resources (screen rendering, writing to disc, network requests etc) Fragment = piece of user interface (similar callbacks to activity) Intent = an operation to be performed (explicit or implicit)
Responsible: • positioning & drawing views • updating layout • component creating (Activity, Service etc.) • garbage collection (pre ART) or GC callbacks (ART) Warning! Never do computation intensive or long lasting tasks on UI thread. Note! You can’t manipulate UI from background thread!
UI • start components • simple datamodel manipulation Do not do in UI thread: • network requests • query a database • long write/read tasks • intensive data model manipulation
Example: activity.runOnUiThread(new Runnable() { public void run() { Log.d("UI thread", "I am the UI thread"); } }); Note! Need to have reference to Activity
each frame has to take less than 16ms Why 60 fps? Human eye can’t make difference past that range Detect less than 60fps rendering on your phone: Settings >> Developer options >> Profile GPU rendering
member variables are discarded But what if we need to restore the previous state after when garbage collector has destroyed our Activity? Example: • user selected value in list • user scroll position • view which is inflated from member variable objects
onSaveInstanceState() is called. Specify data you'd like to save. @Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state savedInstanceState.putInt(STATE_SCORE, mCurrentScore); savedInstanceState.putInt(STATE_LEVEL, mCurrentLevel); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState); }
state data to the onCreate method and the onRestoreInstanceState() method. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Always call the superclass first // Check whether we're recreating a previously destroyed instance if (savedInstanceState != null) { // Restore value of members from saved state mCurrentScore = savedInstanceState.getInt(STATE_SCORE); mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL); } else { // Probably initialize members with default values for a new instance } }
Device Independent Pixel for text Why can’t we use pixels? Different devices have different amount of pixels. Example: Button on device which has 200 pixel height, would be very very tiny on device which has 4000 pixel height Don’t worry: Android OS handles everything for you. Rough measurement: 50dp ~ 1cm
to perform long-running operations • No user interface • Needs to be declared in the AndroidManifest.xml • Must extend the Service class or one of its subclasses • Started with Intent Starting service: Intent intent = new Intent(this, HelloService.class); startService(intent); Example: audio playback, look for new push messages etc.
can we share data between them? Answer: Content providers: interface for querying data • Can be protected with permissions • Needs to be defined in AndroidManifest.xml • Unique path for each content provider ex: “content: //com.example.myapp/students" Example: phonebook
app can can send broadcasts and listen for broadcasts • Broadcasts can be local (in your app only) or global to every app in the phone • Has to be defined in manifest • Broadcasts received as Intent objects • Needs to extend BroadcastReceiver Example: OS battery low, screen unlocked, Wifi connected/connection lost etc.
</intent-filter> </receiver> Receive: public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // do something here } }