6

I want to insert some data into Firebase. for that, I have a non-composable function and in that function, I'd like to call Toast.makeText . . in the .addOnSuccessListener part. However, there's no way for me to get the context that should be in the Toast.makeText statement

fun saveActivityToFB(
    answer: String,
    question: String,
    id: String
) {

    var db: DatabaseReference = Firebase.database.reference
    val ques = Question(answer, question)

    db.child("activity").child("test").child(id).setValue(ques)
        .addOnSuccessListener {
            Log.d("FB", "OK")

           //problems with context here!!
            Toast.makeText(context, "Successfully Added to FB", Toast.LENGTH_SHORT).show()

        }
        .addOnFailureListener {
            Log.d("FB", "Not inserted into FB")
        }
} 

I know that in order to display Toast from composable function, I should get context as:

val context = LocalContext.current

But have no idea how to get the context in this case.

3
  • 2
    If you want to display toast from saveActivityToFB itself, you can pass Context as another argument in this function. Commented Dec 12, 2021 at 16:15
  • Would you please be so kind and show your suggestion in code . . Thank you Commented Dec 12, 2021 at 17:27
  • Yeah, I got it as you suggested. I am having quite some problems with understanding Context . . . Thank you Commented Dec 12, 2021 at 22:59

2 Answers 2

7

If you're going to call that function from a composable function, make it composable and access it via LocalContext.current. If you're calling it from a ViewModel, you can make it an AndroidViewModel and use the ApplicationContext instead. You shouldn't access a Context otherwise. Think about its name—"Context"—the circumstances that form the app state. Following this wavelength, it becomes easy to understand where you should access the Context: you can't just get it out of nowhere, you need to retrieve it from somewhere UI-related. Hence, try doing one the aforementioned methods. If none fit, please provide more information on where you're calling that function.

Sign up to request clarification or add additional context in comments.

Comments

-2
@Composable
fun Test(){
    val context = LocalContext.current // here no problem getting context
    var db: DatabaseReference = Firebase.database.reference
    db.child("activity").child("test").child(id).setValue(ques)
    .addOnSuccessListener {
        Log.d("FB", "OK")

       //use context from outside of the scope here
        Toast.makeText(context, "Successfully Added to FB", Toast.LENGTH_SHORT).show()

    }
    .addOnFailureListener {
        Log.d("FB", "Not inserted into FB")
    }

} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.