Flex Mobile persist view data - actionscript-3

I built a simple hello world app to check out the Flash Builder 4.5 mobile capabilities.
Here's how it works:
The Default View asks for name in an textinput and has a continue button
When you click the continue button it pushes a new view in the viewNavigator which just displays "Hello " + name in a label.
When you click anywhere in this view, it pops a view (i.e. itself) from the viewNavigator, to go back to the default view
I see only 1 issue with this:
When I get back to the default view, it is in its initial state, i.e. the textInput is blank. It seems as if the viewNavigator created a new view of the default view's class and pushed this, instead of just removing the top view and displaying the previous one.
I see this being especially problematic for programs which display data in a grid and you can click the data to view the detail...when you get back, the grid will be empty.
Any ideas or gotchas to solve this?
EDIT:
Project name: HelloWorld
Code below:
HelloWorldDefaultView.mxml
protected function button1_clickHandler(event:MouseEvent):void {
navigator.pushView(HiView, tName.text);
}
HiView.mxml
protected function view1_clickHandler(event:MouseEvent):void {
navigator.popView();
}
protected function view1_creationCompleteHandler(event:FlexEvent):void {
lblHello.text="Hello " + data;
}
Screenshots
Initial screen
Screen 2
Clicking on screen 2 gets us back to initial screen. Notice the blank textInput

Have you tried to set destructionPolicy="never" and then
protected function button1_clickHandler(event:MouseEvent):void {
data = tName.text;
navigator.pushView(HiView, tName.text);
}
to store the data in current View's data - before changing to another one?

That's the way it is supposed to work for mobile applications.
Check out this article: Flex 4.5 (Hero) – Persistant Data in MobileApplication
As they write:
Each time a View is removed from the display list (via popView() or pushView()) its instance is destroyed, but its data model is stored in memory.
In order to save session state for a View, you must modify the data property. This property will be requested when destroying the current instance of the View class. And the data property value will be assigned back to a newly created instance of the same View class when navigating back to that view.

Related

Pause UI in WinRT 8.1

I can't for the life of me get the following functionality to work:
User taps item
Item's image becomes visible via changing visibility property of image
After a short period of time image becomes invisible again (with no user input) via changing the
visibility property
Or, more simply:
Make visible UI change
Pause so user can see UI change
Reverse step 1's UI change
Step 2 happens before steps 1 and 3 regardless of where the code is because the UI is not updating until the logic finishes (I assume).
I am setting the visibility of the image via data binding with INotifyPropertyChanged. All works as expected except when I'm trying to introduce the pause.
I'm trying to pause with this simple method:
private void Pause()
{
new System.Threading.ManualResetEvent(false).WaitOne(1000);
}
It does pause, but the UI changes wait until after that pause even though a change to the bound data happen befores the pause is called, and the other change after.
I have tried using the dispatcher, but it doesn't change anything, and I don't understand it enough:
await dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
{
clickedCard.IsFaceDown = false; // makes the image visible via data binding
}
);
Pause();
I think I need to do something with threading, but I am going in circles.
Any ideas?
You should never do something like this inside the UI thread of your app:
new System.Threading.ManualResetEvent(false).WaitOne(1000);
There are various reasons for not doing it, but in your particular case the problem is that XAML only re-draws once your event-handler completes. So basically this happens:
The item is invisible
Your event handler is called
You set it to visible (but the UI doesn't refresh yet)
You freeze the thread for a second
You set it to invisible again
The event-handler completes
Now the UI updates based on the current value (which is invisible)
I suggest you look at building a Storyboard to do this - Blend can help. See here.

Page constructor called multiple times - Windows Phone 8

I have a page in a WP8 application, that every time I navigate to it, the constructor is called.
From what I know, the constructor of a page called only once at the first time the page loaded. my page is very heavy, and every construction takes wasted time..
this my navigation code, usual one:
NavigationService.Navigate(new Uri("/Views/Pages/ContentControlNew.xaml", UriKind.Relative));
and this is the constructor of the page:
public ContentControlNew()
{
InitializeComponent();
}
Not special.. is it normal that the constructor is called every time? Please tell me if you need more details because I don't know what else to say about this subject.
Yes this is normal because whenever you use NavigationService.Navigate it always creates a new page object and adds that (pushes it) to the navigation stack. For example when you use GoBack() it pops it out of the stack and destroys it, but when it gets back to the previous page it doesn't call the constructor since that one was already in the stack and does not have to be recreated.
If you don't want to create a page every time you navigate to it, you should look into Navigation Models for Windows Phone for some ideas on how you can tackle this.

Windows Phone 8 (icon inside longlistselector datatemplate issue)

I have an application which uses a data template to display items in a longlistselector. Inside the data template there are two images. One of the images, has a loaded event handler which checks if the image should be visible or not. This works perfectly and the image doesn't show up when not needed however when the user locks the screen and unlocks it or when they press the windows key and then return to the app it's all messed up. The image appears on places that it shouldn't. When normally navigating this does not occur. Also the image loaded event doesn't trigger when the user unlocks the phone or comes back after having pressed the windows key. Any help would be appreciated.
Use DataBinding to control the visibility (bind each item to an ItemViewModel)and also you could listen for the Application::Activated event if for some reason your UI state needs refreshing.
You should use visibility converter to manage that it will work good
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int count = (int)value;
if (count != 0)
{
return true;
}
return false;
}

Adobe flex popup single instance

I need to create a flex popup which would be a single instance but we can make it visible and invisible whenver we want to display it. I am not sure we can implement this kind of functionality using createPopup or addpopup method. Instance must be one and need to update it every time some event happen and make it visible or invisible.
thanks
createPopUp requires a class name. All the internals of creating the popup are in that method. You won't be able to use createPopUp with an existing instance of a window. However, when you call createPopUp, the results you get will be the instance of the new popup you just created.
However, addPopUp does accept an instance of an already creating component. You'll want to proceed in one of a few ways:
1) if the popup instance exists; use addPopUp; otherwise use createPopUp:
if(myPopUp){
PopUpManager.addPopUp(myPopUp, etc...)
} else {
myPopUp = PopUpManager.createPopUp(this, myPopUpClassName, etc..);
}
2) Create the popup yourself and always use addPopUp
if(!myPopUp){
myPopUp = new myPopUpClass();
}
PopUpManager.addPopUp(myPopUp, etc...);
Whenever you want to hide the pop up, do so using the removePopUp() method. This method will not destroy the pop up instance, just remove it from view.
PopUpManager.removePopUp(myPopUp);
You're going to have to figure out how to store the reference to your popup outside of the PopUpManager.
And I warn you that all the code I wrote here is psuedo code.

switching between uiviewcontrollers without a UI navigator control

I want to switch between 2 UIViewController classes programmatically without any extra UI control like a UITabBarController that adds a UI to the application.
My main loads the first view controller with addSubView.
vc1 = new viewc1();
window.AddSubview(vc1.View);
window.MakeKeyAndVisible ();
I can load my second viewcontroller from the first one with PresentModalViewController
vc2 = new viewc2();
PresentModalViewController(vc2, true);
but i need to switch back and forth, and to release the old viewControllers to save memory.
What is the best way to do this?
DismissModalViewControllerAnimated(false); in the 2nd view controller isnt releasing memory and I dont want modal "windows" as it doesnt seem optimal. I have a custom UI so the tabbar controller is not wanted.
You can do it in simple code. But you can't release the view controllers as it required to handle user interactions such as button tap events etc. Adding a view to window will only preserve view instance. If you release your view controller instance, you could get a bad access error or unrecognized selector error.
So let your main code be
if(vc1==nil)
vc1 = new viewC1();
window.addSubView(vc1.view);
window.MakeKeyAndVisible ();
And your switch code will be
if(vc2==nil)
vc2 = new viewC2();
if(vc1.view.superview!=nil){
vc1.view.removefromSuperView();
window.addsubview(vc2.view);
} else {
vc2.view.removeFromSuperView();
window.addsubview(vc1.view);
}
Now in dealloc method add
vc1.release();
vc2.release();
Thats it...Hope this helps...
I just followed your syntax
You can use a UINavigationController still, you don't need the extra UI that it provides. You can use the .Hidden property of the UINavigationBar to hide that. To switch between views you can just use PushViewController(controller, animated) to push the new view. If you want to release the old controller then you can just set the UINavigationController's .ViewControllers property using:
navigationController.ViewControllers = new UIViewController[1]{ vc2 };
this will remove the reference of the first controller and make the second controller the root. (this will also work the other way around!)