1.1.18.3. fejezet, Activity

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.QRCodeScanner"
        tools:targetApi="31">
 
        <activity
            android:name=".QRCodeScannerActivity"
            android:label="@string/app_name"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
    </application>
 
</manifest>

Fontos az intent-filter-ben az android.intent.action.MAIN és a android.intent.category.LAUNCHER paraméterek. Ha no-activity-vel készül a projekt, majd új activity-t adunk a hozzá és ezt szeretnénk elindítani a program indulásakor.

Navigáció Activity-k között

        binding.btnLoginWithQRCode.setOnClickListener(){it:View ->
            val intent = Intent(this,QRCodeScannerActivity::class.java)
            intent.putExtra("qrcode", result.getContents()); //Paraméter átadás
            startActivity(intent)
        }

Visszatérési érték kezelése

        val activityLoginWithQRCodeLauncher =
            registerForActivityResult<Intent, ActivityResult>(
                ActivityResultContracts.StartActivityForResult()
            ) { result: ActivityResult? ->
                if (Activity.RESULT_OK != result?.resultCode) {
                    Toast.makeText(this, "Scan cancelled", Toast.LENGTH_LONG).show()
                } else {
                    Toast.makeText(this, "Scanned: " + result.data?.getStringExtra("qrcode"), Toast.LENGTH_LONG)
                        .show()
                }
            }
 
    override fun onCreate(savedInstanceState: Bundle?) {
...
        binding.btnLoginWithQRCode.setOnClickListener(){it:View ->
            val intent = Intent(this, QRCodeScannerActivity::class.java)
            activityLoginWithQRCodeLauncher.launch(intent)
        }
...
    }

Visszatérési érték megadása

            Intent intent = new Intent();
            if (result.getContents() == null) {
                setResult(RESULT_CANCELED,intent);
            } else {
                intent.putExtra("qrcode", result.getContents());
                setResult(RESULT_OK,intent);
            }
            finish();