Blog

Android – Faoliyat «Activities»

Uncategorized @uz

Android – Faoliyat «Activities»

Activity xuddi oyna yoki Java ramkasi kabi foydalanuvchi interfeysiga ega bitta ekranni aks ettiradi.Android faoliyati ContextThemeWrapper sinfining subklassidir.

Agar siz C, C ++ yoki Java dasturlash tili bilan ishlagan bo’lsangiz, unda sizning programmangiz main () funktsiyasidan boshlanganligini ko’rgan bo’lishingiz kerak . Shunga o’xshash tarzda, Android tizimi o’z dasturini onCreate () qayta qo’ng’iroq qilish usuli bilan qo’ng’iroqdan boshlab Activity bilan boshlaydi . Activityni boshlaydigan qayta qo’ng’iroq qilish usullarining ketma-ketligi va quyidagi Activity davrining diagrammasida ko’rsatilgandek Activityni buzadigan qayta qo’ng’iroq usullarining ketma-ketligi mavjud: ( rasm iltifot: android.com )

Android Faoliyat davri

Activity klassi quyidagi qo’ng’iroqlarni, ya’ni voqealarni belgilaydi. Qayta qo’ng’iroq qilishning barcha usullarini amalga oshirishingiz shart emas. Biroq, ularning har birini tushunishingiz va ilovangiz foydalanuvchilar kutganidek ishlashini ta’minlaydigan dasturlarni amalga oshirishingiz muhimdir.

NQayta qo’ng’iroq qilish va tavsif
1onCreate ()

Bu birinchi qayta qo’ng’iroq va Activity birinchi marta yaratilganida chaqiriladi.

2onStart ()

Ushbu qayta qo’ng’iroq, harakat foydalanuvchiga ko’rinadigan bo’lganda chaqiriladi.

3onResume ()

Bu foydalanuvchi dastur bilan o’zaro aloqani boshlaganda chaqiriladi.

4onPause ()

To’xtatib qo’yilgan Activity foydalanuvchi ma’lumotlarini qabul qilmaydi va biron bir kodni bajara olmaydi va joriy Activity to’xtatilayotganda va oldingi Activity qayta tiklanganda chaqiriladi.

5onStop ()

Ushbu qayta qo’ng’iroq, harakat endi ko’rinmay qolganda chaqiriladi.

6onDestroy ()

Ushbu qayta qo’ng’iroq harakat tizim tomonidan yo’q qilinishidan oldin amalga oshiriladi.

7onRestart ()

Ushbu qayta qo’ng’iroq, uni to’xtatgandan so’ng, Activity qayta boshlanganda chaqiriladi.

Misol

Ushbu misol sizga Android ilovalari hayotining aylanish jarayonini ko’rsatish uchun oddiy qadamlardan o’tadi. Biz Hello World Example bobida yaratgan Android dasturini o’zgartirish uchun quyidagi amallarni bajaring –

QadamTavsif
1Siz Android dasturini yaratish uchun Android studiyasidan foydalanasiz va uni Hello World misoli bobida aytib o’tilganidek com.example.helloworld to’plami ostida HelloWorld deb nomlaysiz .
2Asosiy Activity faylini MainActivity.java- ni quyida aytib o’tilganidek o’zgartiring. Qolgan fayllarni o’zgarishsiz saqlang.
3Android emulyatorini ishga tushirish va ilovada kiritilgan o’zgarishlar natijasini tekshirish uchun dasturni ishga tushiring.

Quyida src / com.example.helloworld / MainActivity.java o’zgartirilgan asosiy Activity faylining mazmuni keltirilgan . Ushbu fayl hayot tsiklining asosiy usullaridan har birini o’z ichiga oladi. Log.d () usuli log xabarlarni generatsiya qilish ishlatilgan –

package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;

public class MainActivity extends Activity {
   String msg = "Android : ";
   
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.d(msg, "The onCreate() event");
   }

   /** Called when the activity is about to become visible. */
   @Override
   protected void onStart() {
      super.onStart();
      Log.d(msg, "The onStart() event");
   }

   /** Called when the activity has become visible. */
   @Override
   protected void onResume() {
      super.onResume();
      Log.d(msg, "The onResume() event");
   }

   /** Called when another activity is taking focus. */
   @Override
   protected void onPause() {
      super.onPause();
      Log.d(msg, "The onPause() event");
   }

   /** Called when the activity is no longer visible. */
   @Override
   protected void onStop() {
      super.onStop();
      Log.d(msg, "The onStop() event");
   }

   /** Called just before the activity is destroyed. */
   @Override
   public void onDestroy() {
      super.onDestroy();
      Log.d(msg, "The onDestroy() event");
   }
}

Activity klassi barcha interfeys komponentlarini loyihaning res / layout papkasida mavjud bo’lgan XML fayli yordamida yuklaydi . Quyidagi bayonot UI komponentlarini res / layout / activity_main.xml faylidan yuklaydi :

setContentView(R.layout.activity_main);

Ilovada hech qanday cheklovlarsiz bir yoki bir nechta Activity bo’lishi mumkin. Sizning arizangiz uchun aniqlagan har qanday Activity AndroidManifest.xml faylingizda e’lon qilinishi kerak va sizning ilovangiz uchun asosiy Activity <intent-filter> bilan MAIN amal va LAUNCHER toifasini o’z ichiga olgan manifestda quyidagicha e’lon qilinishi kerak:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tutorialspoint7.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Agar sizning harakatlaringizdan biri uchun asosiy harakat yoki ishga tushiruvchi toifasi e’lon qilinmasa, u holda sizning uy belgingiz Asosiy ekrandagi ilovalar ro’yxatida ko’rinmaydi.

O’zgartirilgan Salom Dunyomizni ishga tushirishga harakat qilaylik ! dasturni biz o’zgartirdik. O’ylaymanki, siz atrof-muhitni sozlash paytida AVD- ni yaratgansiz . Ilovani Android studiyasidan ishga tushirish uchun loyihangizning Activity fayllaridan birini oching va Eclipse Run belgisiasboblar panelidagi Ishga tushirish belgisini bosing . Android studio dasturni AVD-ga o’rnatadi va ishga tushiradi va agar sozlamalaringiz va ilovangizda hamma narsa yaxshi bo’lsa, u Emulator oynasini aks ettiradi va siz Android studiyasidagi LogCat oynasida quyidagi jurnal xabarlarini ko’rishingiz kerak –

08-23 10:32:07.682 4480-4480/com.example.helloworld D/Android :: The onCreate() event
08-23 10:32:07.683 4480-4480/com.example.helloworld D/Android :: The onStart() event
08-23 10:32:07.685 4480-4480/com.example.helloworld D/Android :: The onResume() event

 

Android LotCat oynasi

Android emulyatorida ekranni qulflash tugmachasini bosishga urinib ko’ring va u Android studiyasidagi LogCat oynasida quyidagi voqea xabarlarini hosil qiladi :

08-23 10:32:53.230 4480-4480/com.example.helloworld D/Android :: The onPause() event
08-23 10:32:53.294 4480-4480/com.example.helloworld D/Android :: The onStop() event

Android emulyatorida ekraningizni qulfdan chiqarishga yana bir bor urinib ko’ring va u Android studiyasidagi LogCat oynasida quyidagi voqea xabarlarini hosil qiladi :

08-23 10:34:41.390 4480-4480/com.example.helloworld D/Android :: The onStart() event
08-23 10:34:41.392 4480-4480/com.example.helloworld D/Android :: The onResume() event

Keling, yana Android Orqa tugmasiAndroid emulyatoridagi Orqaga tugmachasini bosishga urinib ko’ring va u Android studiyasidagi LogCat oynasida quyidagi voqealar to’g’risida xabarlarni hosil qiladi va bu Android ilovasi uchun Faoliyat davrini yakunlaydi .

08-23 10:37:24.806 4480-4480/com.example.helloworld D/Android :: The onPause() event
08-23 10:37:25.668 4480-4480/com.example.helloworld D/Android :: The onStop() event
08-23 10:37:25.669 4480-4480/com.example.helloworld D/Android :: The onDestroy() event

Leave your thought here

Email manzilingiz chop etilmaydi. Majburiy bandlar * bilan belgilangan

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Compare
Wishlist 0
Open wishlist page Continue shopping