RouteObserver use and Widget disposing when navigatin to another route with Flutter - flutter

I'm currently developing an app which uses ads.
I've created an Adbanner for testing in the main view.
In the main view i have a lot of buttons that send the user to other routes. Most of this buttons are in another classes and used elsewhere inside the app. I know that to dismiss or dispose the banner i just need to call Dispose() or Dismiss().
My problem is that i'm lookin for a way to dismiss or dispose the bannerAd on the Navigator.push()
I found this and this that doesnt answer my question.
Thanks in advance to anyone who can help

Related

How not to dispose Stateful widget in Flutter?

I have an app. Its homepage has a lot of functionality(heaver). It takes a little bit time to build. Therefore when App starts I show the Splash screen in the meantime Homepage is built. But when I navigate(by push replacement) from the homepage to another it's dispose method called. So when I try to come back to the homepage it takes some time and the app got hang for that time. So I want that When I navigate(by push replacement) from Homepage to another page its dispose method should not be called. So, when user want to come back to the homepage it doesn't rebuild.
Note: I can not use Navigator.push or something else except push replacement because I am navigating from navigation bar( I made my custom Navigation Bar Where I cannot use anything else except push replacement).

Is the back button the same as dispose() method?

flutter
Is the back button the same as dispose() method ?
I know there's not much similarity, but I mean, when you hit the back button, does that free up memory from the tree of the page we left?
Considering that you're talking about the Framework's BackButton widget, then eventually, why?
Because the framework's back button is just an abstraction of creating your own widget with an IconButton that calls Navigator.maybePop(context); when it is pressed.
To answer your question: yes, if the page is closed when BackButton is pressed, then yes it will release resources (it will call dispose() if your widget is a stateful widget).
Keep in mind that your still need to override the dispose() method and release any resource (if any) so that those resources are release when the back button is pressed.
To sum up::: BackButton simply calls Navigator.maybePop(context); and it will only release resources of your StatefulWidget IF you overrode it AND call some_class.dispose().
No the back button doesn't replace the dispose function if you have controllers in a statefulwidget screen when state object is removed you will need to dispose it to avoid memory leaks.
flutterdartmethodscallbackdispose

How to reset variable to default values in getx?

I am creating an app that has multiple questions (like a quiz) and I am using Getx for state management.
in the main method I initialize controllers as follows
void main() {
Get.put(HomeScreenController());
Get.put(QuestionsController());
runApp(MyApp());
}
in the home screen I have a pageview and I am using HomeScreenController() to manage it.
The last page of the page view has a button to go back to the initial page. For my case, I want to reset all variables in both controllers to their initial values.
I have tried one solution, it worked but I feel like it is tedious to do for other controllers that has so many variables which is calling a method that reset the variables manually.
The other solution that I have tried is to dispose the controllers when the user clicks on the button calling Get.put(HomeScreenController()); in the build method of the home screen.
onPressed: () {
qCon.dispose();
controller.dispose();
Navigator.pushReplacementNamed(context, HomeWrapper.id);
},
but I got an error that says
A HomeScreenController was used after being disposed.
'Once you have called dispose() on a HomeScreenController, it can no longer be used.
is there anyway to do what I am trying to do other than updating variable manually?
Update: Turns out that
Get.reset();
do the job when initializing controllers in the home screen
also calling
Get.put(ControllerName());
in a build method will re-initialize the controller

How to Dispose a ViewModel after Popping a page with Xamarin.Forms?

What I would like to do is unsubscribe from events at the moment the ViewModel is no longer needed. I tried implementing IDisposable but nobody calls Dispose(), not Xamarin.Forms nor Prism.Forms.
We have an app created with Xamarin.Forms. We use Prism.Forms to do MVVM. When navigating to a new page (push on stack) Prism.Forms wires the ViewModel to the Page. When navigating back (pop from stack) the ViewModel gets GarbageCollected after a while.
The problem is however that at a point in time we have a couple of the same type of ViewModels with subscriptions to events that are not bound to a View. When the events fire all these ViewModels start doing their thing. So I am looking for a way to unsubscribe at the moment the subscription is no longer needed.
Does anyone have a solution?
You can make sure the Dispose() is called in the OnDisappearing() event of the View, if you want to ensure that ViewModel is not present anymore in the memory than the view.
It is better if you care only about subscribe and unsubscribe of events, then to do it in the OnAppearing() and OnDisappearing(). In that case you will be sure of no event handlers been present on the viewmodel once view is not visible.
Implement IDestructible or INavigationAware
(or both as in BaseViewModelsample from Prism).
Depending on your object lifecycle :
Implement your disposal code in the Destroy method of IDestructible interface.
Implement your disappearing/appearing code in the OnNavigatedFrom/OnNavigatedTo methods on INavigationAware interface.
Bonus:
IDestructible can be implemented also by the View (and it will called accordingly by Prism when the view is destroyed).
Note:
While the solution above using OnAppearing/OnDisappearing works, it induces that ViewModel will depends on a call from View for managing its lifecycle (not clean). Moreover these methods don't exist on ContentView.

Android's viewDidLoad and viewDidAppear equivalent

Does Android have an equivalent to Cocoa's viewDidLoad and viewDidAppear functions?
If not, then how would I go about performing an action when a View appears? My app is a tabbed application, in which one of the tabs is a list of forum topics. I would like the topic list to be refreshed every time the view appears. Is such a thing possible in Android?
The Activity class has onCreate and onResume methods that are pretty analagous to viewDidLoad and viewDidAppear.
Activity.onResume
EDIT
To add to this, since some have mentioned in the comments that the view tree is not yet fully available during these callbacks, there is the ViewTreeObserver that you can listen to if you need first access to the view hierarchy. Here is a sample of how you can use the ViewTreeObserver to achieve this.
View someView = findViewById(R.id.someView);
final ViewTreeObserver obs = someView.getViewTreeObserver();
obs.addOnPreDrawListener(new OnPreDrawListener() {
public boolean onPreDraw() {
obs.removeOnPreDrawListener(this);
doMyCustomLogic();
return true;
}
});
onResume() is more like viewCouldAppear. :) public void onWindowFocusChanged(boolean) is the closest to viewDidAppear. At this point within the activity lifecycle you may ask the view about its size.
From my limited, nascent understanding of Android, you implement viewDidLoad type functionality in the onCreate method of your Activity:
onCreate(Bundle) is where you
initialize your activity. Most
importantly, here you will usually
call setContentView(int) with a layout
resource defining your UI, and using
findViewById(int) to retrieve the
widgets in that UI that you need to
interact with programmatically.
The equivalent for viewDidAppear is closer to the onResume method:
Called after
onRestoreInstanceState(Bundle),
onRestart(), or onPause(), for your
activity to start interacting with the
user. This is a good place to begin
animations, open exclusive-access
devices (such as the camera), etc.