3. Activity
An activity represents the visual representation
of an Android application. activities use views, i.
e. user interface widgets as for example
buttons to create the user interface and to
interact with the user. An Android application
can have several activities.
9. Workshop: Trace Activity Life Cycle
You can use Log or Toast to check life cycle of
activity on each methods;
● onCreate
● onStart
● onResume
● onPause
● onStop
● onDestroy
10.
11. Activity Layout
The user interface for Activities is typically
defined via XML files (layout files).
12.
13.
14. Trick: Single Task
Prevent create instance of Activity, you can
identify launchMode in AndroidManifest.xml
● force for single instance
○ android:launchMode="singleInstance"
● force for single task
○ android:launchMode="singleTask"
● force app to recognize latest state
○ android:alwaysRetainTaskState="true"
15. Trick: Screen Orientation
You can identify the screen orientation by
config in AndroidManifest.xml
● Portrait
○ android:screenOrientation="portrait"
● Landscape
○ android:screenOrientation="landscape"
● Landscape with No Keyboard
○ android:configChange="orientation|kyboardHidden"
16. Multiple Activities
Single App has at least 1 Activity, complex app
has multiple activity. You can call to another
activity using Intent.
17. Workshop: Two Activities
Create App with 2 Activities each activity has a
button for navigate to other activity. Using
Intent to call another activity.
Intent i = new Intent(MainActivity.this,SecondActivity);
startactivity(i);
18.
19. Workshop: Sent data between activity
Create App with 2 Activities, first activity has
text field and button, after push button it'll sent
data in text field to the second activity. Using
putExtra method to create a variable and
identify value;
Intent i = new Intent(getApplicationContext(),
NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
20. Sent a value
Using putExtra method to create a variable and
identify value;
Intent i = new Intent(getApplicationContext(),
NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
21. Receive a value
Using getExtras methods to receive value from
variable;
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
22.
23. Workshop: Temperature Converter
Create multiple Activity App, temperature
converter between Celsius and Fahrenheit
using the following formula
°C x 9/5 + 32 = °F
(°F - 32) x 5/9 = °C