11. Activity Lifecycle
$
¢ Managed by ActivityManager
Developer says what happens at each state
12. Activity Lifecycle
First time run
D/MyActivity( 1146): onCreate
D/MyActivity( 1146):
onStartD/MyActivity( 1146):
onResume
Open another activity, then Back button
D/MyActivity( 1146):
onClickAnotherActivityD/MyActivity( 1146):
onPauseD/MyActivity( 1146):
onStopD/MyActivity( 1146):
onRestartD/MyActivity( 1146):
onStartD/MyActivity( 1146): onResume
17. Declaring the Activity
Let your application know about your Activity into the AndroidManifest.xml
<manifest ... >
<application ... >
<activity android:name=".MyActivity" />
...
</application ... >
...
</manifest >
18. Declaring the Activity
Let your application know about your Activity into the AndroidManifest.xml
<manifest ... >
<application ... >
<activity android:name=".MyActivity" />
...
</application ... >
...
</manifest >
For your main activity use Intent Filters
<manifest ... >
<application ... >
<activity android:name=".MyActivity" >
<intent-filter>
<action android:name= "android.intent.action.MAIN" />
<category android:name= "android.intent.category.LAUNCHER" />
<intent-filter />
<activity />
</application ... >
...
</manifest >
20. Building Android UI
XML Programmatically
Declare UI in XML VS. Initialize new widgets
Inflate XML in Java files Customize properties for each
21. Building Android UI
XML Programmatically
Declare UI in XML VS. Initialize new widgets
Inflate XML in Java files Customize properties for each
Use them both
32. Services
Run in background
Don’t have UI
Run on the UI thread
33. Services
Run in background UI Activity
Don’t have UI startService(); stopService();
Run on the UI thread Service
34. Service Lifecycle
Service starts and "runs" until it
gets a request to stop
To offload work from main thread, use intent
service.
Intent service uses worker thread,
stops when done with work.
35. Service Example
package ro.gdgcluj.demoapp;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
static final String TAG = MyService.class.getSimpleName();
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
return START_STICKY;
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
}
}
36. Declaring the Service
Called via its class name
<service android:name=".ServiceDemo"></service>
Called via action
<service android:name=".IntentService">
<intent-filter>
<action android:name="example.intent.action.IntentService" />
</intent-filter>
</service>
38. Broadcast Receivers
Intent based publish-subscribe mechanism
Listening system events: incoming calls, SMS messages a.o.
39. Broadcast Receivers
Intent based publish-subscribe mechanism
Listening system events: incoming calls, SMS messages a.o.
Register for certain intents
Get notified when intent happens
40. Broadcast Receiver Example
package ro.gdgcluj.demoapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class Receiver extends BroadcastReceiver {
static final String TAG = Receiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive action: "+intent.getAction() );
}
}
41. Registering the Broadcast Receiver
Declaring it in AndroidManifest.xml
<receiver android:name=".ReceiverDemo">
<intent-filter>
<action android:name="example.intent.action.Receiver" />
</intent-filter>
</receiver>