BBB : Back Browser Button
In this tutorial we’ll create our own supprot for back button in Adobe Flex applications. We have a very usefull class in Flex Framework - HistoryManager.
In fact, we just have to register a class in HistoryManager, and implement 2 methods inside it, saveState and loadState. See it in action.
By the way Accordion and TabNavigator have HistoryManager supprot by default.
Part I : Using Accordion and TabNavigator
Well this is the really easy part, everything is ready to use, lets just create new Flex Project, and write following to the MXML file:
[ftf h=”250″]
Let compile this, and start pressing tabs, after you clicked 10 or so times, and check Back/Forward buttons of your browser. As we can see TabNavigator supports browser buttons by default, lets try same thing with Accordion:
[ftf]
Compile, click on headers and use browser buttons, this works pretty much the same here. But lets make our own completely useless component with browser buttons support.
Part II: Building own component with HistoryManager support
Here is something you should know, in our component we’ll use two different kinds of states: visual states and history states, if you have not worked with them before, here is your chance to get to know them better. Our component will contain a Panel container and a LinkBar control. Via LinkBar users will be able to swtich between visual states (in fact, we’ll just resize the panel ;) )
Ok lets start, first create new Flex Project and call it “HistoryManagement”, now lets create new MXML Component in package: eu.orangeflash.examples.history.gui, now lets create ActionScript code file, we’ll write our script there:
[ftf]// historyCode.as
import mx.managers.HistoryManager;
import mx.events.ItemClickEvent;
//event handler for onCreationComplete, we register our component in HistoryManager class
private function onCreationComplete():void
{
HistoryManager.register(this);
}
//event handler for ItemClickEvent.ITEM_CLICK
private function stateChanged(event:ItemClickEvent):void
{
//change current visual state
currentState = event.label;
//change title of the panel
panel.title = event.label;
//save current history state
HistoryManager.save();
}
//this method inherited from the IHistoryManagerClient, we use it to pass our history state object to the HistoryManager class
public function saveState():Object
{
var result:Object = {st:currentState};
return result;
}
//this method will be invoked when user presses back/forward buttons, it retrieves history state object from
//HistoryManager class, and then we apply properties to our component
public function loadState(state:Object):void
{
if(state == null)
{
state = {st:”first”};
}
if(state != currentState)
{
panel.title = currentState = state.st;
}
}[/ftf]
We will import this script file to our MXML component, sience we have some code here I think it’s better to move it to another file. Lets move to our MXML Component and setup some stuff there, open “design mode” and put Panel container on the workspace, than assign following porperties:
ID: panel
title: History
HorizontalAlign: center
VerticalAlign: top
Width: 400
Height: 300
Now drag LinkBar to the Panel and assign following porperties
ID: bar
Data Provider:{[’first’,’second’,'third’]}
Now lets look at “States” panel, you will see “
[ftf]
creationComplete="onCreationComplete();" >
[/ftf]
Since I dont like appearence of the MXML comments, I’m going to explain everything here, as you can see in line 14 we “import” our script class, so now our script class is a part of this component, we could also insert all this script in a
Our component implements mx.managers.IHistoryManagerClient interface (line 4), and contains saveState and loadState methods, which we’ve already implemented.
Well now lets open main MXML file and type there
[ftf h=”100″]
[/ftf]
October 3rd, 2006 at 10:49 am
Great article with a useful code example! I’ll post a link to this on the FD forum.
Flexy.
October 3rd, 2006 at 6:10 pm
Thanks :)