last day (53 days later) » 

7:36 AM
0
Q: Android Snackbar: No suitable parent found from the given view. Please provide a valid view

VanessaFI have a Fragment class and a normal HelpFunctionsclass. I want the Fragment class to show a Snackbar by calling a method in the HelpFunctionsclass. In the Fragment class I call the following code after a certain button has been pressed: HelpFunctions.showSnackBar(getActivity(), getString(R.strin...

 
The solution may be to use Snackbar.make(context, view, text, duration) Because you are using it outside the View layer, you need to pass the context.
 
@CommonsWare: Thanks for your answer. I tried to use findViewById in my Fragment class (which is a UI class) and there I get the same error. Further, in the documentation of Snackbar you see, that it expects a View. By using binding.getRoot() as an argument I pass a view to it as you can read in the official documentation of ViewBinding (developer.android.com/topic/libraries/view-binding). So I don't understand why this error occurs with the Illegal Argument
@EmmanuelConradie: Thanks for your answer. The question is what is the context and how can I create and pass it? Furthermore, there is a constructor in SnackBar that does not need any context and I try to use this one with 3 arguments that I pass to it. Unforunately I still get the error message.
 
@VanessaF you already have the context, it is the activity you are passing
 
@EmmanuelConradie: What is then the view? I thought the activity is the view? Or shall it serve as both the view and the context?
 
@VanessaF Try the updated answer
 
7:36 AM
@EmmanuelConradie: Does not work. Yields the same error.
 
@VanessaF could you please specify which line you are getting the error on? Because I don't think the problem is the currentSnackbar. I think it is when you are trying to get the snackbar view
Have you made sure your root view has loaded correctly before passing it?
 
@EmmanuelConradie: How can I make sure that the root view has loaded correctly? I just use the following command binding.getRoot() and that should actually do it. What else can I do?
 
@VanessaF please post the code of how you are loading your view and where you are calling show snackbar
 
@EmmanuelConradie: Unforunately the code is quite long (more than 1000 lines). So I don't think it makes sense to post it. But do you know what I can do to ensure that the"root view has loaded correctly before passing it" as you wrote? The error comes from the line Snackbar currentSnackBar = Snackbar.make( activity, root, message, Snackbar.LENGTH_LONG);. I don't think that I need an activity here because on other occasions in the app I never use the activity and so far everything has been okay. Strangely in this case this IllegalArgumentException occurs and I don't know why. Any suggestions?
 
@VanessaF your first problem is that your view class is 1000 lines long. Your view should not contain any logic. Call showSnackbar right after you called onBind(view)
@VanessaF I do not know in which method you are binding or inflating your view so without code I cannot help you
 
7:38 AM
Hi Emmanuel. I am binding my view in the onCreateView method of the Fragment by using the code binding = FragmenRateBinding.inflate(inflater, container, false);. This works fine (I am always doing this).
 
7:57 AM
Maybe the problem is that I call it in a Firebase Database method which mostly work asynchronously
 
8:25 AM
So I found out that the problem is in fact the asynchronous methods of the Firebase Realtime Database. When trying to call the snackbar from a normal method it works. The question now is if there is a way to also call the snackbar from an asynchronous mehtod?
 
 
2 hours later…
10:53 AM
Yes, you can simply call:
activty.runOnUiThread(showSnackBar(...));
Keep in mind you will not have this problem if you adhere to the rule that a view layer should not contain business logic.
You should be updating LiveData from your firebase call and then observing it in your view layer(activity)
 
 
1 hour later…
12:21 PM
Thanks Emmanuel for your answer. Where do I have to call activty.runOnUiThread(showSnackBar(...));? And currently I am not using LiveData for my Firebase calls because its makes things quite complex and creates many additional classes. Is there a way of doing this without live data? Can I just call the method activty.runOnUiThread(showSnackBar(...)); without live data?
You wrote "Keep in mind you will not have this problem if you adhere to the rule that a view layer should not contain business logic. " --> But when using Firebase in your ViewLayer you need to implement some business logic in the Fragment as far as I see it.
 
 
7 hours later…
7:37 PM
@EmmanuelConradie: Any comments to my last comments?
 
8:05 PM
If you are in the activity class then you call this.runOnUiThread
You should not be using firebase in the view layer.
Take a look at how to use view models. You will keep having problems if you are putting everything in a view layer. Watch a tutorial on Youtube that shows how to build a complete project that would be a good starting point.
MVVM is the best structure to be using, as recommended by Google
 

  last day (53 days later) »