I have an activity with a launchMode of singleTop but I want it to refresh when I go up to it from a child activity. Currently, because my fragment (which contains the UI) is instantiated in the activity's onCreate(), the UI doesn't reload when returning to it. But if I instantiated the fragment in onStart() it would be called when the activity is restarted. While I've gained understanding of the Activity lifecycle, I still don't fully understand how it and the Fragment lifecycle must coexist. I instantiate the fragment in the activity's onCreate() because that's what the tutorial app did.
Anyway, would this work?
I'm asking this in search of a potential solution to this question, which I asked but no one has answered. Thanks.
You can do that, but that's not efficient.
I would suggest to create a refresh method inside your fragment, and that refresh method can update some UI components in your fragment, which is very fast and simple. In your activity onStart(), you can find the fragment (that you instantiated in onCreate) with the fragment manager and call the fragment's refresh. One of the reason is efficiency if you don't need to recreate a fragment, then avoid it. Instantiate new fragment takes time.
Bonus: you can set the criteria in the onStart() to determine if you really need the refresh, ie, it has been refreshed 2 seconds ago, do you really want to refresh again?
Related
I am trying to send data from one ViewModel to another using Messaging Centre.
I have subscribed the event in 2nd ViewModel's constructor. But the event is not subscribed as the constructor is not compiled until I open the page/view corresponding to the ViewModel.
I am using MVVM Light, until now I had an understanding that the VM's constructor are compiled when ViewModelLocator is called at the app startup.
Can someone help me understand this better and how can I subscribe the event i.e. compile the constructor without the VM being called.
Perhaps you are thinking about this in the wrong way. Without seeing code it's difficult to see what you are trying to achieve exactly, but what you could do is subscribe to the event elsewhere in your app, for example in your App.xaml.cs. When the event fires, at that point navigate to a new page of type ViewModel2 and pass any details required as a navigation parameter.
Hopefully there is an obvious answer to this that my brain at 2am is not seeing (I'm basing this on the fact I can't find anyone else with the same problem).
When I press the "hardware/built in" back button on my windows phone 7 none of my bindings are re-evaluating. For example on my MainPage I have a button's "IsEnabled" bound to a bool property in my ViewModel that checks if a value is in isolated storage. In the emulator it starts as disabled (obviously). I then navigate to another page and add the required value to isolated storage. When i press the hardware back button to return to the original page the binding does not get re-evaluated and my button remains disabled.
However if I go back to MainPage via code using the navigate method the binding gets re-evaluated and everything is good.
Any ideas as to why?
The issue sounds like the RaisePropertyChanged method isn't being raised by your MVVM Light ViewModel - if you're binding Get{} accessor of your IsEnabled property on the viewmodel directly to a value in IsolatedStorage, then the ViewModel isn't going to know that that underlying value has necessarily changed.
When you write the value to IsolatedStorage, you can use the Messenger interface in MVVM to notify the ViewModel-in-question that it should fire the RaisePropertyChanged() event for your model, and that will re-bind that property in your view.
When you use the NavigationService.Navigate method, you are actually performing a forwards navigation and a new instance of your page is created. When you press the hardware back button, a backwards navigation is performed and the previous page is re-displayed. Almost all of the time, what you get is a cached version of the original instance of the page.
You could re-evaluate any bindings in the OnNavigatedTo override for the page, but this introduces a fair bit of 'code smell'. As mentioned above, using the Messenger will enable you to update the associated view model, which will update the binding on the page.
I'm looking for a way to intercept the ASP.NET processing pipeline in such a way to be able to register event handlers to all events on the Page class. The reason is, I need to maintain a session-bound instance of a component that needs to be notified of all important Page events, starting from OnPreInit.
There's an arbitrary number of Page descendants in the application, which are not under my control. Hence I cannot use an approach like using a single custom descendant, that would notify the session-bound component, as a base class for all pages in the web application.
I don't think creating a custom IHttpHandler or IHttpModule implementation would solve the problem. Also note I cannot create a custom HttpApplication descendant.
It isn't going to be an elegant process to do what you are looking at, especially if you need to handle multiple page events, but in theory it is fully possible from within the Global.asax to setup handlers that you need for each and every page.
The trick here is to add your code to the global.asax in the PreRequestHandlerExecute method, from here you can get access to the HttpApplication object, get access to the page from there, and then register your events. This process is necessary as a new page instance is created for every page that is processed.
Now, other options as you know are far more elegant, but this should get to where you need to be. One helpful tutorial I found although around Themeing shows you the whole process here.
EDIT:
After seeing your comment, yes, you can simply do what I'm stating above, in a custom HttpModule. The article I linked even shows you that process :)
Without knowing more about what you're trying to accomplish it really sounds like you do indeed want to create a http module or handler. You might want to take a look at this question
I have built a multiplage application in Flex with different user roles. I use a View Stack with a Menu Bar to navigate between the different pages.
However - each time a page gets opened, I need to do some database calls, apply User Role settings, etc. in an init() function. This init function may reference some UI elements of this page.
I tried to load it with the "creationComplete" event, but this one gets only triggered once (since the page is not rebuilt each them the view stack shows it).
Now I have put it on the "show" event, but this seems not to get triggered consistently, or before the page is fully created the first time.
Whats the best practice for this case?
* I have to pass in data
* Call init functions (database calls)
* Manipulate components
Ideally I would need a way to call init each time the page is loaded and after all components are created.
Thanks for your help,
Martin
Just an idea but why not trying to do your remote call on ViewStack change effect. You could use an interface for this that your views implements.
Try updateComplete event.
Dispatched when an object has had its commitProperties(), measure(), and updateDisplayList() methods called (if needed).
This is the last opportunity to alter the component before it is displayed. All properties have been committed and the component has been measured and layed out.
I had the same kind of problem when I wrapped the s:Group with wrapper class to put inside TabNavigator. The result, show event is not being called. Finally I changed all my s:Group with mx:Canvas, directly put them into TabNavigator(The same thing goes for ViewStack too). After that creationComplete event was called once and subsequent page visits call show of mx:Canvas
Do the same best practis rules regarding subscribing/unsubscribing to events apply in asp.net?
I know it might seem like a silly question, but when I think about it, I have never really seen any code where people first subscribe to an event on a page and then unsubscribe later on in the web request.
Example 1:
On a page, in the Page_Load method, I subscribe to a updating event on a ListView. Should I unsubscribe from that event later on, for example in the OnPreRenderComplete method?
Example 2:
In the passive view patter, a view (Page control/Usercontrol) will raise an event whenever it needs the presenter to do anything. So the presenter needs to subscribe to events on the view, but do it also need to unsubscribe from the events again?
Best regards, Egil.
The page instance and all of its components will "go out of scope" when request completes, e.g. they become eligible for GC. So your ListView will go out of scope along with the Page/user controls on it. You don't need to unsubscribe (unless you subscribe to an event that belongs to some kind of singleton that survives every request and use one of the methods of the page as the event handler, for example).
The same thing is valid for the presenter (again as long as this presenter is used solely with one page and goes out of scope after that).
Generally, no. Events are supposed to be dumped automatically when the page unloads. SUPPOSED to be. I've run into a bug before (in .NET 1.1) where that wasn't the case.
I won't bother unsubscribing, unless I notice a problem with the page (like, a method being called 20 times from a phantom in the call stack: that's usually a sign of something not being unsubscribed properly).