Module 4 user interfaces activity

70 349 1
Module 4 user interfaces   activity

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Android Basic Course © 2012 University of Science – HCM City . M.Sc. Bui Tan Loc btloc@fit.hcmus.edu.vn Department of Software Engineering, Faculty of Information Technology, University of Science – Ho Chi Minh City, Viet Nam Module 4: User Interfaces Section 2: Activity Android Basic Course © 2012 University of Science – HCM City . High-level diagram class of the Android View API 2 View ViewGroup Activity … … … … Android built-in layout classes Android built-in view container classes Android built-in single view classes Android Basic Course © 2012 University of Science – HCM City . Objectives • After completing this section, you will able to: • Create an activity with loading layout form xml-based layout file. • Manage the activity lifecycle. • Start or shut down an activity. • Create resources of many types of menus. • Handle menu item selections. • Use event handlers. • Use event listeners. 3 Android Basic Course © 2012 University of Science – HCM City . Contents • Activity overview • Activity skeleton code • Activity lifecycle • Different types of menus • Creating a menu resource • Common Activity’s methods for working with menus • Common Menu’s methods for working with submenus • Common MenuItem’s methods for working with menu items • User Input events • Using event handlers • Using event listeners 4 Android Basic Course © 2012 University of Science – HCM City . Activity Overview • Activity (user interaction): UI component typically corresponding to a screen. • Ex: An email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. • Activities themselves are made up of subcomponents called views. • Activities using Intent Filters and Permissions determine how they interact with each other and with other applications. • Activities must be declared in the Android manifest file. 5 Android Basic Course © 2012 University of Science – HCM City . Designing layout by using xml layout text editor <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> 6 Android Basic Course © 2012 University of Science – HCM City . Designing layout by using xml layout GUI editor 7 Android Basic Course © 2012 University of Science – HCM City . Loading layout from xml-based layout file package com.example.helloandroid; import android.app.Activity; import android.os.Bundle; public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 8 Android Basic Course © 2012 University of Science – HCM City . Creating layout classes by using source code – xml layout built-in source code public class HelloAndroid extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("Hello, Android"); LinearLayout linear; linear = new LinearLayout(this); linear.setOrientation(LinearLayout.VERTICAL); linear.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); linear.addView(tv); setContentView(linear); } } 9 Android Basic Course © 2012 University of Science – HCM City . High-level diagram of activity • Views and other Android components use strings, colors, styles, and graphics, which are compiled into a binary form and made available to applications as resources. • The automatically generated R.java provides a reference to individual resources and is the bridge between binary references and the source code of an Android application. 10 [...]... Android Basic Course Starting an activity for results //NoteList Activity public static final int ACTIVITY_ CREATE = 0; public static final int ACTIVITY_ EDIT = 1; … //Open creating note screen Intent i = new Intent(this, NoteEdit.class); startActivityForResult(i, ACTIVITY_ CREATE); … //Open editing note screen Intent i = new Intent(this, NoteEdit.class); startActivityForResult(i, ACTIVITY_ EDIT); … © 2012 University... Android Basic Course Starting an activity for passing no values • Starting an activity for passing no values: Intent intent = new Intent(this, SignInActivity.class); startActivity(intent); © 2012 University of Science – HCM City Android Basic Course Starting an activity for passing values • Starting an activity for passing values by putting values into intent object: //SignInActivity public static final... launched in front of the activity, the onPause() function is called Later, if the activity is still paused when the OS needs to reclaim resources, it calls onSaveInstanceState() before killing the activity © 2012 University of Science – HCM City Android Basic Course Activity Life Cycle Sample • Open Child Activity > onCreate(null) -> onStart() -> onResume() -> [Open Child Activity] > onSaveInstanceState()...Android Basic Course Activity skeleton code public class ExampleActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // The activity is being created } @Override protected void onStart() { super.onStart(); // The activity is about to become visible } @Override protected void onResume() { super.onResume(); // The activity has become... new onStart() Called when the Activity is becoming visible on the screen to the user onResume() Called when the Activity starts interacting with the user (This method is always called, whether starting or restarting.) onPause() Called when the Activity is pausing or reclaiming CPU and other resources This method is where you should save state information so that when an Activity is restarted, it can... City Android Basic Course Shutting Down an Activity • You can also shut down a separate activity that you previously started by calling finishActivity() This method is used for shutting down an activity being in the foreground startActivityForResult(intent, requestCode); try { Thread.sleep(3500); } catch (InterruptedException e) { e.printStackTrace(); } finishActivity(requestCode); © 2012 University... Science – HCM City Android Basic Course Activity skeleton code … @Override protected void onPause() { super.onPause(); // Another activity is taking focus (this activity is about to be "paused") } @Override protected void onStop() { super.onStop(); // The activity is no longer visible (it is now "stopped") } @Override protected void onDestroy() { super.onDestroy(); // The activity is about to be destroyed... OrderActivity.class); intent.putExtra(this.NAME_KEY, nameValue); intent.putExtra(this.PASS_KEY, passValue); startActivity(intent); © 2012 University of Science – HCM City Android Basic Course Catching passed values //OrderActivity … public void OnCreate(…){ Bundle extras = getIntent().getExtras(); String usrName = extras.getString(SignInActivity.NAME_KEY); String passWord = extras.getString(SignInActivity.PASS_KEY);... Called to stop the Activity and transition it to a nonvisible phase and subsequent lifecycle events onDestroy() Called when an Activity is being completely removed from system memory This method is called either because onFinish() is directly invoked or the system decides to stop the Activity to free up resources © 2012 University of Science – HCM City Android Basic Course Another activity lifecycle... 2012 University of Science – HCM City Android Basic Course Catching returned results //NoteEdit Activity @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Bundle extras = data.getExtras(); switch(requestCode) { case ACTIVITY_ CREATE: if ( resultCode = RESULT_OK){ String title = extras.getString(NotesDbAdapter.KEY_TITLE); . City . Activity Life Cycle Sample • Open Child Activity > onCreate(null) -& gt; onStart() -& gt; onResume() -& gt; [Open Child Activity] > onSaveInstanceState() -& gt; onPause() -& gt; onStop() -& gt;. onCreate(null) -& gt; onStart() -& gt; onResume() -& gt; [Home Button] > onSaveInstanceState() -& gt; onPause() -& gt; onStop() -& gt; [Start App] > onRestart() -& gt; onStart() -& gt; onResume() • Phone. onCreate(null) -& gt; onStart() -& gt; onResume() -& gt; [Phone Call] > onSaveInstanceState() -& gt; onPause() -& gt; onStop() -& gt; [Hang Up or press Back] > onRestart() -& gt; onStart() -& gt; onResume() 18 Android

Ngày đăng: 02/02/2015, 11:13

Từ khóa liên quan

Mục lục

  • Slide 1

  • High-level diagram class of the Android View API

  • Objectives

  • Contents

  • Activity Overview

  • Designing layout by using xml layout text editor

  • Designing layout by using xml layout GUI editor

  • Loading layout from xml-based layout file

  • Slide 9

  • High-level diagram of activity

  • Activity skeleton code

  • Activity skeleton code

  • The activity lifecycle

  • Android Activity main lifecycle methods and their purpose

  • Another activity lifecycle diagram

  • The onSaveInstanceState() function

  • Activity Life Cycle Sample

  • Activity Life Cycle Sample

  • Starting an activity for passing no values

  • Starting an activity for passing values

Tài liệu cùng người dùng

Tài liệu liên quan