1 / 11

Activity trong Android

Activity trong Android. Trần Vũ Tất Bình. Activity?. Activity là một trong 4 thành phần chính của một ứng dụng Android. Activity được dùng để hiện thị một màn hình . Khi làm việc với activity cần bắt đầu với một số kiến thức cơ bản sau : Lifecycle của activity Tạo menu, dialog

tejana
Télécharger la présentation

Activity trong Android

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Activity trong Android TrầnVũTấtBình

  2. Activity? • Activity làmộttrong 4 thànhphầnchínhcủamộtứngdụng Android. • Activity đượcdùngđểhiệnthịmộtmànhình. • Khilàmviệcvới activity cầnbắtđầuvớimộtsốkiếnthứccơbảnsau: • Lifecycle củaactivity • Tạo menu, dialog • Khởiđộngmột activity, liênlạcgiữa 2 activity • Task

  3. Lifecycle của Activity

  4. Tạo menu • Tạp menu (đơngiảncựckỳ): • Chuộtphảivàomộtdòngtrốngtrong Activity • Chọn Source  Override/Implement methods  chọnonCreateOptionsMenuvàonOptionsItemSelected • TrongonCreateOptionsMenu, thêmđoạn code: menu.add(0, 111, 0, "Item1"); • TrongonOptionsItemSelected, thêmxửlýdạng: switch (item.getItemId()) { case 111: //lam gi do break; }

  5. Tạo Dialog • TrongứngdụngApiDemos mở package com.example.android.apis.app  AlertDialogSamples.java • Xem demo cáchgọi dialog, hàm callback vàcáchtạomột dialog.

  6. Khởiđộngmột activity • Dùng Intent: • Khaibáotường minh: cungcấpchínhxácthông tin của activity cầngọi (nếucùngứngdụngchỉcầncungcấptên class, nếuứngdụngkhácnhauthìcungcấptên package, tên class) • Khaibáokhôngtường minh: cungcấpthaotáccầnlàmgì, vớiloạidữliệunào, thaotácthuộcnhómnào… hệthốngsẽtìm activity tươngứngđểkhởiđộng.

  7. Khởiđộngmột activity • Tường minh: đoạn code bêndướisẽtạokhởiđộng Activity tênlàTargetActivity Intent intent = new Intent(getApplicationContext(), TargetActivity.class); startActivity(intent);

  8. Khởiđộngmột activity • Khôngtường minh: đoạn code bêndướisẽkhởiđộngmột activity nàođóđăngcókhảnăngxemảnh. Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivity(intent);

  9. Khởiđộngmột activity • Vớicáchkhởiđộng activity khôngtường minh, bạncầnbiếtmộtchútvề Intent-filter. • Intent-filter sẽgiúpmột activity (chunghơnlàmộtthànhphầnứngdụng) đăngkývớihệthốngmìnhcóthểlàmđượcthaotácgì, trongnhómnào, vớiloạidữliệunào. • Nhưvậykhi intent và intent-filter khớpnhau, activity sẽđượchệthốngkhởiđộng.

  10. Liênlạcgiữa 2 activity • Khikhởiđộngmột activity, tacóthểgửikèmdữliệutrong intent nhưvídụsau: intent.putExtra("value1", new String("Hello")); intent.putExtra(“value2", new Long(100)); • Bênphía activity đượckhởiđộng, cóthểlấydữliệuđượcgửinhưsau: getIntent().getExtras().getString("value1"); getIntent().getExtras().getLong("value2");

  11. Liênlạcgiữa 2 activity • Cóthểkhởiđộngmột activity vớimộtyêucầunàođóvà activity kiakhilàmxongcôngviệcsẽtrảlạikếtquảcho activity trước • Vídụ activity A yêucầumột activity làmgiúpviệcchụpảnh, activity B đápứngđượcviệcnày, saukhi user chụpảnhxongsẽtrảlại file ảnhcho activity A. • Nhưthếsẽđỡtốnnhiềucôngsứclàmmộtviệcmàngườikhácđãlàmrồi.

More Related