gkont
gkont

Reputation: 199

Android, startActivityForResult with arguments (Intent, Int), gives type mismatch error

Trying to turn bluetooth on, I used startActivityForResult with arguments (Intent, Int), as follows.

val REQUEST_ENABLE_BT : Int = 1;

if (bluetoothAdapter?.isEnabled == false) {
    val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
}

I get a type mismatch error for both arguments. "Required Activity - found Intent" for the first argument, "Required Intent - found Int" for the second argument. According to documentation the arguments should be (Intent, Int) as used in the code. I have imported the only suggestion in android studio

import androidx.core.app.ActivityCompat.startActivityForResult

Programming with kotlin, compose. Can somebody help me here?

EDIT: Looks like there are (at least) two different startActivityForResult functions. Android studio (in my project) proposes only the ActivityCompat case for import. I cannot figure out how to import the right one. According to the answers so far, the startActivityForResult that is working with that code is in one of the packages

import androidx.activity.ComponentActivity
import android.app.Activity

But android studio ignores them.

Upvotes: 1

Views: 423

Answers (2)

Skizo-ozᴉʞS ツ
Skizo-ozᴉʞS ツ

Reputation: 20646

I guess the problem is on your import.

The startActivityForResult you are using is this

public static void startActivityForResult(
        @NonNull Activity activity, @NonNull Intent intent,
        int requestCode, @Nullable Bundle options) {
    if (Build.VERSION.SDK_INT >= 16) {
        Api16Impl.startActivityForResult(activity, intent, requestCode, options);
    } else {
        activity.startActivityForResult(intent, requestCode);
    }
}

And you should be using this one from import android.app.Activity

public void startActivityForResult(Intent intent, int requestCode) {
    throw new RuntimeException("Stub!");
}

Upvotes: 0

Vishnu S Dharan
Vishnu S Dharan

Reputation: 669

import androidx.activity.ComponentActivity

It will already imported if you are using Compose.

implementation 'androidx.activity:activity-compose:1.7.2'

In the build.gradle file try implementing dependency activity-compose version 1.7.2, it was working for me.

Upvotes: 0

Related Questions