PAPB SI 4 AndroidActivity
PAPB SI 4 AndroidActivity
PERANGKAT BERGERAK
(MOBILE)
“
Android Activity
K Candra Brata
[email protected]
Mobille App Lab 2017-2018
Android Activity
https://developer.android.com/reference/android/app/Activity.html
Activity
Almost all activities interact with the user, so the Activity class takes care
of creating a UI window for you.
Activity
Example
Activity
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Let's Shop for Food!" />
</RelativeLayout>
2. Define Activity
Java class
…
<application>
<activity android:name=".MainActivity"
android:icon="@drawable/icon"
android:label= “hello apps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
…
Activity Stack
Activities in the system are managed as an activity stack.
When a new activity is started, it is placed on the top of the stack and
becomes the running activity.
The previous activity always remains below it in the stack, and will not
come to the foreground again until the new activity exits.
Activity Stack
OrderActivity
Place order
CartActivity CartActivity
View shopping cart View shopping cart
FoodListActivity FoodListActivity FoodListActivity
Choose food items Choose food items Choose food items
MainActivity MainActivity MainActivity MainActivity
What do you want to do? What do you want to do? What do you want to do? What do you want to do?
Activity State
States Description
Active / The activity is at the top of the Activity Stack.
Running Foreground task visible on the device screen
Paused Activity is still visible but partially obscured, instance is
running but might be killed by the system.
Stopped The activity is currently not visible to the user (in other
words it is totally obscured on the device display by other
activities).
Killed/ The Activity has been terminated by the runtime system in
Destroyed order to free up memory and is no longer present on the
Activity Stack.
Activity State
Activity Life Cycle
!
OnSaveInstanceState – This is invoked by Android when the activity is being destroyed.
Activities can implement this method if they need to persist any key/value state items.
OnRestoreInstanceState – This is called after the OnCreate method is finished, and provides
another opportunity for an Activity to restore its state after initialization is complete.
Maintaining State
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(MyActivityTag,"OnCreate State");
@Override
protected void onStart() {
super.onStart();
Log.i(MyActivityTag,"OnStart State");
}
@Override
protected void onResume() {
super.onResume();
Log.i(MyActivityTag,"OnResume State");
}
http://bit.do/papb_si_b