Reputation: 2204
Intent doesn't work with RestApiService callback.
Previously It worked in RegisterActivity:
fun alert(text: String) {
val intent = Intent(this, PopUp::class.java)
intent.putExtra("text", text)
startActivity(intent)
}
I want to display the result as a pop-up message. PopUp
is a custom activity view.
When I place it into another class, there is an error:
None of the following functions can be called with the arguments
supplied. (Context!, Class<*>!) defined in
android.content.Intent (String!, Uri!) defined in
android.content.Intent
import android.content.Intent
import androidx.core.content.ContextCompat.startActivity
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class RestApiService {
// MY CUSTOM INTENT! WORKED BEFORE IF TO PLACE INTO AN ACTIVITY.
fun alert(text: String) {
val intent = Intent(this, PopUp::class.java)
intent.putExtra("text", text)
startActivity(intent)
}
fun addUser(userData: ModelUserRegister, onResult: (ModelUserRegister?) -> Unit){
val retrofit = ServiceBuilder.buildService(RestApi::class.java)
retrofit.addUser(userData).enqueue(
object : Callback<ModelUserRegister> {
override fun onFailure(call: Call<ModelUserRegister>, t: Throwable) {
onResult(null)
}
override fun onResponse(call: Call<ModelUserRegister>, response: Response<ModelUserRegister>) {
val addedUser = response.body()
if (response.code() in 200..320)
{
alert(response.message())
} else {
alert(response.message())
}
onResult(addedUser)
}
}
)
}
}
The latest research gave me the advice to change:
val intent = Intent(this, PopUp::class.java)
to:
val intent = Intent(RegisterActivity.this, PopUp::class.java)
Didn't help. This question for those people who want to use Kotlin!
Upvotes: 4
Views: 13059
Reputation: 11
You should use applicationContext
instead. There are three contexts an intent or toast will accept. They are applicationContext
, requireContext
, or this
.
Your code should look like this:
val intent = Intent(application Context, Second activity::class.java)
startActivity(intent)
Upvotes: 1
Reputation: 397
You may try this one, if from a fragment:
val intent = Intent(requireContext, PopUp::class.java)
Upvotes: 0
Reputation: 2204
Step1. Considering an absence of information the easiest solution is to use callback
or return
value (String? in my case).
class RestApiService {
fun addUser(userData: ModelUserRegister, onResult: (ModelUserRegister?, String?)-> Unit){
//... code
onResult(addedUser, response.message())
}
}
Step2. Call the returned value from the main Activity
and use an Intent
with alert View
apiService.addUser(userInfo) { modelUserRegister: ModelUserRegister?, s: String? ->
if (s != null) {
alert(s)
} else {
alert("NO TEXT AS A RESULT")
}
}
Still waiting for any suggestions to call the Intent outside the Activity class
.
Upvotes: 0
Reputation: 1324
To create a new Intent object, you have to pass context
as the first parameter. You cannot just use this
anywhere since depending on where you use it, it might not be context
.
You can use this
or this@ActivityName
(ActivityName.this
in Java) only when your codes are in an activity.
You can add a context
parameter to your alert()
function to fix this issue:
fun alert(context: Context, text: String) {
val intent = Intent(context, PopUp::class.java)
intent.putExtra("text", text)
context.startActivity(intent)
}
Upvotes: 4