0% found this document useful (0 votes)
53 views23 pages

PAPB SI 4 AndroidActivity

The document discusses Android activities. Some key points: - Activities display user interfaces and represent single focused tasks. Almost all activities interact with the user through a UI window. - Applications are made up of one or more activities. Each activity is defined as a Java class that typically has a corresponding XML layout file. - The activity lifecycle involves states like active/running, paused, stopped, and destroyed. Proper handling of state is important when activities transition between these states. - Activities are managed on a stack, with the current activity at the top. When a new activity starts, it is placed on top of the previous activity/activities. - The manifest file declares all

Uploaded by

Muchsin Huda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views23 pages

PAPB SI 4 AndroidActivity

The document discusses Android activities. Some key points: - Activities display user interfaces and represent single focused tasks. Almost all activities interact with the user through a UI window. - Applications are made up of one or more activities. Each activity is defined as a Java class that typically has a corresponding XML layout file. - The activity lifecycle involves states like active/running, paused, stopped, and destroyed. Proper handling of state is important when activities transition between these states. - Activities are managed on a stack, with the current activity at the top. When a new activity starts, it is placed on top of the previous activity/activities. - The manifest file declares all

Uploaded by

Muchsin Huda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

PENGEMBANGAN APLIKASI

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

An activity is a single, focused thing that the user can do.

Almost all activities interact with the user, so the Activity class takes care
of creating a UI window for you.
Activity
Example
Activity

 Application is Made up with Activities


 An application might consist of just one or more Activities.
 Java class, typically one activity in one java class file
 An Activity typically has a UI layout.
 Layout is usually defined in one or more XML files
 Activity "inflates" layout as part of being created

 All activities must Specified in the AndroidManifest.xml


Implement new
activities
 Define layout in XML
 Define Activity Java class
 extends AppCompatActivity
 Connect Activity with Layout
 Set content view in onCreate()
 Declare Activity in the Android manifest
1. Define layout
in XML

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout

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

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
} Resource is layout in this XML file

Connect activity with layout


3. Declaring Activity
in Android Manifest

 Every Activity has to be declared inside the <application> tag in the


AndroidManifest.xml - otherwise “Activity Not Found” error condition
occurs . . .
Launcher
Activity
Typically, one of the activities is marked as the first one (launcher activity
or main activity) that should be presented to the user when the application
is launched.

An activity can be declared as launcher activity using intent-filter main


action and launcher category declaration in AndroidManifest.xml


<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.

 a user will be switching activities while using a mobile device.

 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

Managing activities in the system


Activity Stack

Managing activities in the system

After viewing shopping cart, user


decides to add more items, then
places order.

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

 Every Activity has a life cycle.


 Represent the Activity state.
Active and Visible
Lifetimes
Maintaining State

How activities Can save their State?


Maintaining State
Bundle State

!
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

How activities Can save their State?


Modify Your Activity Java Code....

import android.util.Log;

public class MainActivity extends AppCompatActivity {

private static final String MyActivityTag = "lifecycle";

@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");
}

// ......... Continue for another activity States ..........


}
Thanks!
JOIN !!

http://bit.do/papb_si_b

You might also like