THe problem is little difficult to convey, The Actual scenario will be help you guys to understand the real problem.
In the Android Application.
I have a lot of the Jquery Mobile Page append to the android Webview.
When i select one Page (E.g) Profile , the page opens properly and if i press the back button, the application moves to the main page, if i again select profile and press back the application goes to the login page.
if i select some other page and select the profile this is not happening. this issue is not only with the single page. in all the page i have the same issue. Can some one guide me what should i have do?
The Source code of the Key Press event,
enter code here
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && webview.isEnabled()
&& !this.onLogin) {
webview.loadUrl("javascript:handleDeviceBack()");
return true;
} else if (keyCode == KeyEvent.KEYCODE_BACK && this.onLogin) {
moveTaskToBack(true);
}
if (keyCode == KeyEvent.KEYCODE_HOME) {
webview.loadUrl("javascript:handleDeviceHome()");
return true;
}
return false;
}
In my web view,
enter code here
handleDeviceBack = function(status) {
mHealth.util.logMessage('On device Back');
var historyBack = {
"home" : "page",
"loginPage" : "page",
"welcomeMsg" : "page"
};
var moduleIndex = {
"assessNotFound" : "../../home/view/home.html",
"showActivity" : "../../home/view/home.html",
"showMessage" : "../../home/view/home.html",
"show_tracker" : "../../home/view/home.html",
"settingsPage" : "../../home/view/home.html"
};
var graphPages = {
"singlehealthdatapage" : "page",
"multiplehealthdatapage" : "page"
};
var otherShortcuts = {
"show_tracker_view" : "../../trackers/view/showtracker.html",
"detailMessage" : "../../messages/view/showmessage.html",
"alfrescoDIV" : "../../messages/view/showmessage.html"
};
// var exitAppCriteria={ "home" : "page","loginPage" : "page","welcomeMsg" :
// "page"};
if($('.ui-page-active').attr('id')=="condition_index")
{
$.mobile.changePage("../../home/view/history.html");
}
else if (historyBack[$('.ui-page-active').attr('id')]
|| $('body').children().is('#tandcPage')) {
Android.finishActivity();
} else if (moduleIndex[$('.ui-page-active').attr('id')]) {
Android.highlightHome();
$('.ui-alert-wallpaper').detach();
$.mobile.changePage(moduleIndex[$('.ui-page-active').attr('id')]);
} else if (graphPages[$('.ui-page-active').attr('id')]) {
Android.showTab();
Android.pageHistory();
} else if (otherShortcuts[$('.ui-page-active').attr('id')]) {
$.mobile.changePage(otherShortcuts[$('.ui-page-active').attr('id')]);
} else {
$('.dw').detach();
$('.dwo').detach();
$('.dw').detach();
$('.ui-alert-wallpaper').detach();
Android.showTab();
Android.pageHistory();
}
};
I found the problem is with the Android.pageHistory();
enter code here
public void pageHistory() {
this.activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
WebContainerActivity.webview.goBack();
}
});
}
Where First time its running properly but if the function called repeatly the web view.go back to the first page.
In your activity override backpressed method like this
#Override
public void onBackPressed() {
webview.loadUrl(sameurl);
}
Hard coded the URL by their ID and solved the issue temporarily.
Related
I am using 1D scanner i.e "https://www.issyzonepos.com/ipda018-android-7-0-pda-bluetooth-4-1-support-gprs-wifi_p326.html" for my app. The app is basically to scan code and order data. I have also implemented a scanner through a camera. My APP is working perfectly on this device but when I try to run it on a mobile device then it crashes on "sm = new ScanDevice();" as it is a non-scanner device. I tried a lot to resolve this can you tell me how can I solve this. I have used this device's SDK for the scanner.
Code:
sm = new ScanDevice();
public void onKeyDown(int keyCode, KeyEvent event) {
int charCode = event.getKeyCode();
System.out.println("charCode = " + keyCode + " " + event);
//sm.openScan();
if (charCode == 302 || charCode == 301 || charCode == 303) {
if (spBar.getSelectedItem().toString().trim().equals("SELECT BAR")) {
Toast.makeText(getActivity(), getResources().getString(R.string.select_a_bar), Toast.LENGTH_SHORT).show();
} else {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(actSearchCode.getApplicationWindowToken(), 0);
System.out.println("openScannerL = " + sm.getOutScanMode());
sm.openScan();
}
} else if (charCode == 82) {
} else {
sm.closeScan();
}
}
For the sake of completeness, here's the stack trace of the error from the OP - unfortunately, as a screenshot.
Line 76 is sm = new ScanDevice();
As I mentioned in my comment, you can just create your scanner instance inside a try-catch block. For this, you'll have to declare the variable in a class, but instantiate it in a method. Your code still doesn't show where you're declaring your sm, but you may end up with something like this:
public class MyScanActivity extends Activity {
ScanDevice sm;
public void onCreate(Bundle bundle) {
try {
...
sm = new ScannerDevice();
...
} catch (Exception ex) {
Log.w("SCANNER_ACTIVITY", "Could not create scanner device - not a scanner?");
}
...
}
public void onKeyDown(int keyCode, KeyEvent event) {
...
if (sm == null) {
Log.i("SCANNER_ACTIVITY", "Scanner not instantiated - not a scanner?");
} else {
// all your scanning code goes here
}
}
}
I am trying to disabling the physical device back in android only in some screens. Trying the below code is not working. Any idea?
import { RouterExtensions } from "nativescript-angular";
import * as application from "application";
import { AndroidApplication, AndroidActivityBackPressedEventData } from "application";
import { isAndroid } from "platform";
export class ItemComponent implements OnInit {
constructor(private router: Router) {}
ngOnInit() {
if (!isAndroid) {
return;
}
application.android.on(AndroidApplication.activityBackPressedEvent, (data: AndroidActivityBackPressedEventData) => {
data.cancel = true; // prevents default back button behavior
});
}
}
#Override
public void onBackPressed() {
if (!shouldAllowBack()) {
doSomething();
} else {
super.onBackPressed();
}
}
Add this code in your activity-
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Your Code Here. Leave empty if you want nothing to happen on back press.
}
For more information visit this link.
Back button is controlled at Activity level, NativeScript uses one single Activity and all your pages / routes are fragments inside that.
So you don't have to call it on every page / component. Add the listener to your app component and check the router to know which page you are at and cancel the back event.
Use location and check if location consists the desired path(as mentioned in routing file) where you want to use your custom function.
import { Location } from '#angular/common';
constructor(private _location: Location
){}
if (application.android) {
application.android.on(AndroidApplication.activityBackPressedEvent, (data: AndroidActivityBackPressedEventData) => {
const path = this._location.path();
console.log(`path i s--> ${path}`);
switch (path) {
case '/Screen 1':
data.cancel = true;
break;
case '/Screen 2':
//do something else
break;
});
}
We have a launcher application that works fine on older versions of Android. We have a device that is running Android 5.1, and are running into issues.
When pressing the back button from within the application, we allow the user to go to the settings page. Pressing the home key re-launches the application. Pressing the back button on other devices also relaunches the application.
On the new device, pressing the back button allows us to navigate to the Android home page. It does not launch the application.
We are overriding the back button like so:
#override
public void onBackPressed() {
// Display the password prompt if required
if (PreferencesManager.isPasswordPresent()) {
LeaveApplicationPasswordDialogFragment dialog = LeaveApplicationPasswordDialogFragment.getInstance();
dialog.show(getSupportFragmentManager(), "password");
}
else {
// Prompt whether we are about to leave the app
LeaveApplicationDialogFragment dialog = null;
MyApplication application = (MyApplication )
getApplication();
if (application.isDefaultLauncher()) {
dialog = LeaveApplicationDialogFragment.getInstance("Are you sure you want to leave ** to access the device's settings?");
}
else {
dialog = LeaveApplicationDialogFragment.getInstance("Are you sure you want to leave ***");
}
dialog.show(getFragmentManager(), "leaving");
}
}
In the dialog fragment, we accept the confirmation and process it like so:
public void exitToSettings() {
GUIAndroidTouchBaseActivity.this.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
shutdownOperations();
finish();
}
Per some research and other threads, I worked with our exit method like so:
public void exitToSettings() {
Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
GUIAndroidTouchBaseActivity.this.startActivity(intent);
shutdownOperations();
finish();
}
No dice. Same behavior.
What am I missing? Is there something in OS 5.1 that's overriding our launcher? Again, pressing the home button launches the app as expected. Navigating to the home page from the settings page by pressing the back button does not.
What we have works on other devices and OSs. We've had no issue with 4.1 and 6.1.
We are also overriding the back button like so:
#Override
public boolean onKeyDown(int keyCode, KeyEvent KEvent) {
int deviceid = KEvent.getDeviceId();
//Making sure not processing same key again
if (KEvent.getRepeatCount() != 0) {
return true;
}
if (!SettingsOpened) {
int keyaction = KEvent.getAction();
// "Esc" key can not be stooped id diveceid is non zero because it can be back key of android
if (KEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && deviceid != 0) {
return super.onKeyDown(keyCode, KEvent);
}
if (keyaction == KeyEvent.ACTION_DOWN) {
String key = KeyEvent.keyCodeToString(keyCode); //wont work in version 11 or less
if (keyCode != KeyEvent.KEYCODE_ENVELOPE) {
Matcher matcher = KEYCODE_PATTERN.matcher(key);
if (matcher.matches() || ExternalKeyboard.keyMatches(KEvent)) {
int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState());
char character = (char) keyunicode;
//toast.makeText(this, "onKeyDown" + _lastChar + repeatcount, toast.LENGTH_SHORT).show();
_lastChar = character;
_actionDown = true;
ExternalKeyboard.KeyboardAddChar(character);
}
}
}
return true;
}
else {
return super.onKeyDown(keyCode, KEvent);
}
}
Thanks!
Adding
android:stateNotNeeded="true"
android:clearTaskOnLaunch="false"
to my manifest took care of it.
Coding with Android Studio.
I am trying to make the emVideoView work with onKeyDown (or other keys) for keyboard or remote control on a user click to load java activity (Play list).
Here is what I tried. Instead of loading a list it is returning or exiting completely rather than loading an Activity example (VideoSelectionActivity) is my page that have all the play list.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (emVideoView != null && (emVideoView.isPlaying() || emVideoView.getFocusedChild() != null ) &&
(keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_DPAD_DOWN)) {
return emVideoView.onKeyDown(keyCode, event);
} else {
return super.onKeyDown(keyCode, event);
}
}
#Override
public void KeyEvent(VideoSelectionActivity arg0) {
if (emVideoView.isPlaying()==true) {emVideoView.showDefaultControls();}
}
I have changed this many times and the onClick events works but incorrectly also not loading activity.
I am using WizarDroid library, and creating a simple Wizard.
What I am trying to do is at the final step I want to disable Back button at the bottom. I have searched every where and the documentation is not helping me.
Here is my Basic Wizard code:
public class FormWizard extends BasicWizardLayout
{
public FormWizard()
{
super();
}
#Override
public WizardFlow onSetup()
{
return new WizardFlow.Builder()
/*
* Mark this step as 'required', preventing the user from advancing to the
* next step without selecting one option.
*/
.addStep(Form1.class, true)
.addStep(Form2.class, true)
.addStep(Form3.class)
.create();
}
#Override
public void onWizardComplete()
{
super.onWizardComplete();
// Terminate the wizard
getActivity().finish();
}
}
The Form1,Form2 and Form3 are simply extending WizardStep and showing some data to user.
In the documentation the wizard.goNext(); method is defined but it is not available in my scenario.
I simply want to disable the user to go back after they reach at Form3 or final step.
If you are talking about disabling the software/hardware (next to the home button and menu button) then you simply have to handle the onPress yourself. Example:
//FOR API 5-
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(), "Can't go back", Toast.LENGTH_LONG).show();
}
//FOR API 6+
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Toast.makeText(getApplicationContext(), "Can't go back", Toast.LENGTH_LONG).show();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}