Back Button in webView - android-activity

When the user clicks on back button, I've implemented the following code that works very well for links inside of my webview:
#Override
public void onBackPressed() {
if (this.webViewFragment != null && this.webViewFragment.canGoBack()) {
this.webViewFragment.goBack();
} else {
super.onBackPressed();
}
}
My problem is that I have native menu in Android, and when the user clicks on the menu Profile for example and then clicks on the menu Dashboard, the back button doesn't work. Nothing happens. As I said before, just works for links clicked inside of the webview.
Anyone knows a solution for that?

or try this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

Related

how to disable gwt.material.design.client.ui.MaterialLink on specific situation?

I have this problem, I prepare a page with GWT, with the Materialink disabled.
my Java view:
#UiField
MaterialLink exportLink;
my view.xml:
<m:MaterialLink ui:field="exportLink" text="Esporta Report" addStyleNames="btn" target="_blank" float="RIGHT" enabled="false"/>
The button is disabled and the click not work (GOOD).
if the user complete the Form, I will enable it.
exportLink.setHref(updatedUrl());
exportLink.setEnabled(true);
and work perfert.
When I try to disable it becouse the user put a wrong value.
exportLink.setHref(null);
exportLink.setEnabled(false);
the button looks ok but is possible to click it.
Any suggest?
I found this solution:
public class MyMaterialLink extends MaterialLink {
#Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
if(!enabled){
if (getElement().getAttribute("href") != null) {//per prevenire doppi disabled che cancellano l'href-disabled
getElement().setAttribute("data-href-disabled", getElement().getAttribute("href"));
getElement().removeAttribute("href");
}
}else {
getElement().setAttribute("href",getElement().getAttribute("data-href-disabled"));
getElement().removeAttribute("data-href-disabled");
}
}
#Override
public void setHref(String href) {
if(!isEnabled()){
getElement().setAttribute("data-href-disabled",href);
}else {
super.setHref(href);
}
}

Prevent Reload Activity / Webview when go Back to app and when press the turn off button

I have a fragment inside of an Activity and then, inside of this fragment I also have a webview.
Problem: After lock my screen and unlock pressing the turn off button, my Activity is recreated or my webview is reloaded (not sure what happens).
The same problem occurs when I switch apps in my device.
I tried to change my configChanges and my launchMode. Here is part of my activity in android manifest file:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize"
android:launchMode="singleTop"
>
</activity>
Also I tried to check my savedInstanceState on myCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
this.init(); // Initialize my webview
}
...
EDIT:
#Override
protected void onResume() {
super.onResume();
//Check if the user is authenticated onResume
String message = getIntent().getStringExtra(MainActivity.FragmentIdentifier);
if(message == null || message.compareTo(MainActivity.showWebViewFragment) == 0){
changeFragment(new WebViewFragment());
}else if (message.compareTo(MainActivity.showLoginFragment) == 0){
changeFragment(new LoginFragment());
}
}
Any ideas or code samples will be appreciated, thanks!
I figured out that I should handle the Instance and check if already saved or not, otherwise my onResume could always reload my webview. Here my new code:
#Override
protected void onResume() {
super.onResume();
//Check if the user is authenticated onResume
String message = getIntent().getStringExtra(MainActivity.FragmentIdentifier);
if (message == null || message.compareTo(MainActivity.showWebViewFragment) == 0) {
/*
Check if the instance is saved. If yes, is not necessary to create a new instance of the webview,
otherwise will always reload.
*/
if (!getSavedInstance()){
changeFragment(new WebViewFragment());
}
} else if (message.compareTo(MainActivity.showLoginFragment) == 0) {
changeFragment(new LoginFragment());
}
}
I declared a new global variable
private Boolean savedInstance = false;
And here my get and set
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setSavedInstance(true);
}
public Boolean getSavedInstance() {
return savedInstance;
}
public void setSavedInstance(Boolean instance_saved) {
this.savedInstance = instance_saved;
}

GWT: How to disable the anchor link event when clicked

I want to disable the anchor link event when it clicked one time. I used anchor.setenabled(false) but nothing happend. When I click the same button again the event e is true. I want false at that time.
public void onCellClick(GridPanel grid, int rowIndex, int colindex,EventObject e)
{
if(rowIndex==0 && colindex==2){
tomcatHandler = "Start";
anchorStart.setEnabled(false);
}else if(rowIndex==0 && colindex==3){
tomcatHandler = "Stop";
****anchorStop.setEnabled(false);
anchorStart.setEnabled(false);
anchorRestart.setEnabled(true);****
}else if(rowIndex==0 &&colindex==4){
tomcatHandler = "Restart";
anchorRestart.setEnabled(false);
}
AdminService.Util.getInstance().tomcat(tomcatHandler,new AsyncCallback<String>() {
#Override
public void onSuccess(String result) {
imageChangeEvent(result);
}
#Override
public void onFailure(Throwable caught) {
}
});}
Anchors in GWT have always had a problem with setEnabled() because HTML doesn't support such a property. A quick workaround is to create a new widget that subclasses GWT's Anchor, adding the following override:
#Override
public void onBrowserEvent(Event event) {
switch (DOM.eventGetType(event)) {
case Event.ONDBLCLICK:
case Event.ONFOCUS:
case Event.ONCLICK:
if (!isEnabled()) {
return;
}
break;
}
super.onBrowserEvent(event);
}
This disables the passing of the browser event to GWT's Anchor class (summarily disabling all related handlers) when the link is double clicked, focused or clicked and is in a disabled state.
Source
It doesn't seem to actually disable the anchor, but it does retain the status that has been set with anchor.setEnabled(), so just test that within your handler e.g.
myAnchor.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent evt) {
// write to filter and then call reload
if (((Anchor) evt.getSource()).isEnabled()) {
//do stuff
}
}
});

how to disable Search Button- Android

I am displaying a dialog while launching the app, and user has to click on that dialog to move on for next screens, so dialog should not close if user press back/search buttons of the device.
dialog.setCancleble() is working for back button but not for search button.
So, what should I implement to achieve this?
You have to override the Key Event in your Activity. Here is a little snippet which catches few Key Events,
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// PhysicalMenuClicked=true;
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
// CustomDialog.exitApp_Dialog(context);
}
if(keyCode==KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0)
{
return true; //true means that we are handling the event here.
}
return true;
}

Suppress control-click context menu, gwt 1.6

My gwt 1.6 application intercepts mouse clicks on hyperlinks, so when a user shift-clicks on links to "authors" they get an Edit... dialog box instead of navigating to the author's page. That's working nicely.
I'd now like to allow the user to control-click to select more than one author, but I can't figure out how to suppress the browser's default popup menu. This code handles shift-clicks correctly, but fails in the hosted browser when I control-click and half-fails in Firefox (handleCtrlClick() gets called, but I still get the browser menu):
public void onModuleLoad() {
Event.addNativePreviewHandler(this);
}
//
// Preview events-- look for shift-clicks on paper/author links, and pops up
// edit dialog boxes.
// And looks for control-click to do multiple selection.
//
public void onPreviewNativeEvent(Event.NativePreviewEvent pe) {
NativeEvent e = pe.getNativeEvent();
switch (Event.getTypeInt(e.getType())) {
case Event.ONCLICK:
if (e.getShiftKey()) { handleShiftClick(e); }
if (e.getCtrlKey()) { handleCtrlClick(e); }
break;
case Event.ONCONTEXTMENU:
if (e.getCtrlKey()) { // THIS IS NOT WORKING...
e.preventDefault();
e.stopPropagation();
}
break;
}
}
A breakpoint set inside the ONCONTEXTMENU case is never called.
IIRC ctrl + click is the correct way to select multiple items not ctrl + right click unless you're using a one button mouse (iMac), in that case I can't help you.
Could you provide more details?
Edit:
Why not override the contextmenu (e.g. disable it) then create your own context menu widget (perhaps based on vertical MenuBar + MenuItems) and display it only on Ctrl + RightClick?
In other words you'd create a MouseHandler somewhat like this (pseudo code):
public void onMouseDown(MouseDownEvent event) {
Widget sender = (Widget) event.getSource();
int button = event.getNativeButton();
if (button == NativeEvent.BUTTON_LEFT) {
if(event.is_ctrl_also)
{
// Add to selection
selection = selection + sender;
}
else
{
// Lose selection and start a new one
selection = sender;
}
}
else if(button == NativeEvent.BUTTON_RIGHT) {
if(event.is_ctrl_also)
{
// show context menu
this.contextmenu.show();
}
else
{
// do something else
}
}
return;
}
I've not encountered the bug with Ctrl-Leftclick firing a ContextMenu event, but I'm sure you could also make a workaround for Firefox only using permutations.
I'm getting closer:
public void onModuleLoad() {
Event.addNativePreviewHandler(this); // Catch shift- or control- clicks on links
addContextMenuEventListener(RootPanel.getBodyElement());
}
protected native void addContextMenuEventListener(Element elem) /-{
elem.oncontextmenu = function(e) {
return false; // TODO: only return false if control key down...
};
}-/;
That disables the right-click menu entirely; I'd really like to disable it ONLY if the control key is pressed...