Professional Documents
Culture Documents
PB - 3 - Android UI & Event
PB - 3 - Android UI & Event
(MOBILE)
“
Activity & Intent
K Candra Brata
andra.course@gmail.com
Mobille App Lab 2019-2020
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>
…
Launcher Activity
• What happen if you place intent category launcher on both activity?
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
onRestart() – Called when the activity is about to restart after having previously been
stopped by the runtime system.
onStart() – Always called immediately after the call to the onCreate() or onRestart()
methods, this method indicates to the activity that it is about to become visible to the
user. This call will be followed by a call to onResume() if the activity moves to the top
of the activity stack, or onStop() in the event that it is pushed down the stack by
another activity.
onResume() – Indicates that the activity is now at the top of the activity stack and is
the activity with which the user is currently interacting
@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");
}
Typically, one of the activities is designated as the first one (main) that
should be presented to the user when the application is launched.
ACTION_DIAL , tel:123
Display the phone dialer with the given number filled in.
ACTION_VIEW , http://www.google.com
Show Google page in a browser view. Note how the VIEW action does what is
considered the most reasonable thing for a particular URI.
ACTION_EDIT , content://contacts/people/2
Edit information about the person whose identifier is "2".
ACTION_VIEW , content://contacts/people/2
Used to start an activity to display 2-nd person.
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="btn1Click"
android:text="Button 1"></Button>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:onClick="btn2Click"
android:text="Button 2"></Button>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="btn3Click"
android:text="Button 3"></Button>
</LinearLayout>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OR
// shortest way to recieve data..
String data = getIntent().getExtras().getString("keyName");
// shortest way to recieve data..
String data = getIntent().getStringExtra("keyName");
Explicit Intent
Secondary Attributes
MainActivity
Explicit Intent
Example
SecondActivity
Explicit Intent
Example
Intent
• Starting Activities and Getting Results
• The startActivity(Intent) method is used to start a new activity, which will be
placed at the top of the activity stack.
• Sometimes you want to get a result back from the called sub-activity when it
ends.
For example, you may start an activity that let the user pick a image from
galery; when it ends, it returns the image that was selected.
Intent
• In order to get results back from the called activity we use the method
• If a child activity fails for any reason (such as crashing), the parent activity will
receive a result with the code RESULT_CANCELED.
Intent
MainActivity.Java
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if((requestCode== 0) && (resultCode == Activity.RESULT_OK)) {
String selectedImage= intent.getDataString();
Toast.makeText(this, selectedImage, Toast.LENGTH_LONG).show();
http://bit.do/PB_Tekom