Timer is still firing after Clicking on other Links in GWT - gwt

So I have a timer and it keeps on firing even though I cleared the Panel and loaded other model... my question is, how to cancel a timer when I unload a model?
So here is part of my code
public Display(List<Clarification> result) {
if (result.size() == 0) {
Window.alert("EMPTY");
} else {
RootPanel.get("Dev1").clear();
t = new Timer() {
public void run() {
cd = new ClarificationDispatcher();
cd.getClarificationsCount(result.size());
}
};
t.scheduleRepeating(5000);
}
I tried to cancel the Timer onUnload() method however, I don't believe it is getting called at all...
Thanks!

Steps to follow
use window.onunload event that is called when page is refreshed
first export cancelTimer() method to java script using JSNI and register cancelTimerFunction as java script function that is called on page unload
cancel timer on window close
Code:
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
private static Timer timer = null;
public void onModuleLoad() {
exportCancelTimer();
final Label label = new Label("Hello ");
timer = new Timer() {
#Override
public void run() {
label.setText("Hello " + Math.random() * 100);
}
};
timer.scheduleRepeating(500);
RootPanel.get().add(label);
Window.addCloseHandler(new CloseHandler<Window>() {
#Override
public void onClose(CloseEvent<Window> event) {
timer.cancel();
}
});
}
public static void cancelTimer() {
if (timer != null) {
System.out.println("cancel");
timer.cancel();
}
}
public static native void exportCancelTimer() /*-{
$wnd.cancelTimerFunction = $entry(#com.x.y.z.GWTProject::cancelTimer());
$wnd.onunload = $wnd.cancelTimerFunction;
}-*/;

Related

Second activity is still start on handler although press back button for quiting app

My application has an Introduce activity that show process bar before using app.
pb = (ProgressBar) findViewById(R.id.pb_loader);
final Handler h = new Handler() {
#Override
public void handleMessage(Message message) {
pb.setVisibility(View.INVISIBLE);
Intent it = new Intent(FirstIntroActivity.this, SecondIntroActivity.class);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);
}
};
h.sendMessageDelayed(new Message(), 3000);
But after I press BACK button to exit application, my phone is turn back to application and go to SECOND activity ( after 3000ms ). How to resolve this error?
Alternative is to use a Timer to schedule start of your second activity.we can cancel starting the second activity by cancelling timer in OnBackPressed() callback.
private Timer timer;
#Override
protected void onResume() {
super.onResume();
timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
// add code to start your second activity
}
}, 2000);
}
#Override
public void onBackPressed() {
timer.cancel();
super.onBackPressed();
}

GWT Fire ValueChangeEvent after a specific time

I have an webapp with a large collection. So my user get a text box to filter the collection.
But every time the user put in one letter the filter action starts. This is very slow some times.
So I want only get the value change event when the user stop typing for one second.
I tried it this way:
#Override
public void onValueChange( ValueChangeEvent<String> event )
{
Timer t = new Timer()
{
#Override
public void run()
{
addChangeHandler( new ChangeHandler()
{
#Override
public void onChange( ChangeEvent event1 )
{
ValueChangeEvent.fire( TextBoxPSG.this, getValue() );
}
} );
}
};
t.schedule( 15000 );
}
But this doesn't work.
Maybe someone has an Idea or the same problem.
Thanks in advance.
Do not use ValueChangeEvent. Use KeyUpEvent.
private static Timer timer = new Timer() {
#Override
public void run() {
// do you filter work;
}
};
...
myTextBox.addKeyUpHandler(new KeyUpHandler() {
#Override
public void onKeyUp(KeyUpEvent event) {
if (timer.isRunning()) {
timer.cancel();
}
timer.schedule(1000);
}
});

When is Widget OnUnload() is called?

Can you please tell me when a widget's OnUnload() is called?
I tried to override it however, it is never been accessed. Also what is the best way to unload a composite, I am using RootPanel.get("Dev1").clear();
If you want to stop the timer when you leave the page then use CloseHandler.
This handler is called while page closing and refreshing.
Window.addCloseHandler(new CloseHandler<Window>() {
#Override
public void onClose(CloseEvent<Window> event) {
timer.cancel();
}
});
If you want to stop the timer when clearing a vertical panel then use removeFromParent.
This overridden method is to be called when any widget is removed form its parent.
private Timer timer = null;
/*
* This is the entry point method.
*/
public void onModuleLoad() {
final Label label = new Label("Hello ") {
#Override
public void removeFromParent() {
if (timer != null && this.isAttached()) {
timer.cancel();
System.out.println("timer is stopped");
}
super.removeFromParent();
}
};
timer = new Timer() {
#Override
public void run() {
label.setText("Hello " + (int) (Math.random() * 100));
}
};
timer.scheduleRepeating(500);
final VerticalPanel verticalPanel = new VerticalPanel();
verticalPanel.add(label);
Button button = new Button("Remove Label");
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
label.removeFromParent();
}
});
verticalPanel.add(button);
RootPanel.get().add(verticalPanel);
}
As per you comments try below code where clear method is overridden for VerticalPanel.
final VerticalPanel verticalPanel = new VerticalPanel(){
#Override
public void clear(){
if(this.isAttached()){
timer.cancel();
}
super.clear();
}
};
verticalPanel.getElement().setId("Div1");

gwt client session time out

I am using gwt 2.3 with gwtp framework.In this application I wan to maintain a session time of 5 mins.This means if current user is not doing up to 5 min and he comes after five min then on his first event/action on screen a he should be be logged out.
In gwt there is class named Timer which can be used in this issues.But I am not getting how to recognize action of user on the screen.I did google on it, & found the code for gwt-ext.Below is the code of gwt-ext
Ext.get(“pagePanel”).addListener(“click”, new EventCallback() {
#Override
public void execute(EventObject e) {
MessageBox.alert(“On Mouse Click”);
}
});
Ext.get(“pagePanel”).addListener(“keydown”, new EventCallback() {
#Override
public void execute(EventObject e) {
MessageBox.alert(“On Key Press Click”);
}
});
In above code tag in working properly so I am attaching link from where I got this code.here
Same type of code I am looking in gwt.If there any other better way to do this then please let me know. Thanks in advance
If action/event can be really everythin, I would solve it with a
NativePreviewHandler in the following way:
boolean expired;
final Timer logoutTimer = new Timer() {
#Override
public void run() {
expired = true;
}
};
NativePreviewHandler nph = new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
if (!expired) {
logoutTimer.cancel();
logoutTimer.schedule(300000);
} else {
// do your logout stuff here
}
}
};
Event.addNativePreviewHandler(nph);
If the user shell be logged out without a new action after 5 minutes:
final Timer logoutTimer = new Timer() {
#Override
public void run() {
// do your logout stuff here
}
};
NativePreviewHandler nph = new NativePreviewHandler() {
#Override
public void onPreviewNativeEvent(NativePreviewEvent event) {
// Of course do this only when logged in:
logoutTimer.cancel();
logoutTimer.schedule(300000);
}
};
Event.addNativePreviewHandler(nph);

GWT : print button

I am trying to create a button that prints the current browser window.
This is my current code, that uses (or at least it tries to use) JSNI:
private Button print = new Button(constants.print(), new ClickHandler() {
#Override
public void onClick(final ClickEvent event) {
/*-{
if ($wnd.print) {
$wnd.print();
return true;
} else {
return false;
}
}-*/
}
});
But when I click the button, nothing happens. It is my first GWT application, so I am not sure about how to implement it.
new Button(constants.print(), new ClickHandler() {
#Override
public void onClick(final ClickEvent event) {
print();
}
private native boolean print( ) /*-{
if ($wnd.print) {
$wnd.print();
return true;
} else {
return false;
}
}-*/; });
Should work! Always place JSNI within a native method.
Since GWT version 1.5, there's a built-in print function:
import com.google.gwt.user.client.Window
public class PrintHandler implements ClickHandler {
public void onClick (ClickEvent event) {
Window.print()
}
}
Here is my 2 cents:
Create a re-usable class:
public class PrintHandler implements ClickHandler {
public void onClick (ClickEvent event) {
print();
}
private native boolean print ()
/*-{
if ($wnd.print) {
$wnd.print();
return true;
} else {
return false;
}
}-*/;
}
And use it anywhere you like:
new Button( constants.print(), new PrintHandler() )