Broadcast receiver crashes when i try to open MainActivity.kt

the code in onReceive function

override fun onReceive(context: Context, intent: Intent) {
Log.i(“Receiver”, “This is broadcast receiver”)
Toast.makeText(context, “Language changed”, Toast.LENGTH_SHORT).show()
context.startActivity(Intent(context, MainActivity::class.java))
}

log message -
Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

at com.myapp.broadcastreceiverinkotlin.LocaleChangedReceiver.onReceive(LocaleChangedReceiver.kt:14)

app crashes when it tries to reopen MainActivity.kt

Just as the error suggests.
you need to add a flag to the intent.

you can use

context.startActivity(Intent(context, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))

to start the activity

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.