App looses focus after a dialogAlert is shown - android

I am working on a dialog, and once I call it, there is no focus on it.
I am working on a xamarin app.
Android.App.AlertDialog.Builder alertDialog = new Android.App.AlertDialog.Builder(this);
alertDialog.SetTitle("Special Notification Access");
alertDialog.SetMessage(
"You have no special app access permission for the Notifications!\n" +
"Please go to Settings -> Apps -> Special app access -> Notification access " +
"and give permission to this app."
);
alertDialog.SetPositiveButton("OK", (senderAlert, args) => {
Android.Util.Log.Debug(TAG, "Pressed OK");
});
alertDialog.SetNegativeButton("No", (senderAlert, args) => {
Android.Util.Log.Debug(TAG, "Pressed No");
});
Dialog diag = alertDialog.Create();
diag.Show();
Once I press a button on my remote (I am working on an Android TV, SDK 28) the dialog seems to get focus, but the log shows that the app has lost focus, see here:
09-10 13:37:24.974 12053 12053 W ViewRootImpl[MainActivity]: Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_DPAD_CENTER, scanCode=353, metaState=0, flags=0x8, repeatCount=0, eventTime=13590499, downTime=13590499, deviceId=6, source=0x301 }
After that, the remote stops working, nothing is done by pressing the buttons. Also, once I open the settings (via ADB comand) and go back to the app, the dialog is in focus, and everything works fine.
I found various solutions on the net, including this:
How to set focus to android alert dialog negative button?
Which uses the following code:
diag.getButton(AlertDialog.BUTTON_NEGATIVE).requestFocus();
This method does not exit anymore.
Also, I found this:
alertDialog.setOnShowListener(new DialogInterface.OnShowListener(){
#Override
public void onShow(DialogInterface dialog) {
Button positive= alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
positive.setFocusable(true);
positive.setFocusableInTouchMode(true);
positive.requestFocus();
}
});
But again, the setOnShowListener does not exist anymore.
What should I do?
Please let me know, if you need any more info.
Thank you for your time.
Edit1: Added picture

Related

Login App wevbiew Exit Error (Back Button doesnt go to the dashboard and make works the custom exit)

I have made a webview apk in which we login first and then after authentication we see the dashboard
the problem is when i use the back button it directly shows the exit dialog box that i made instead of going back to the previous and i even tried to alter my code so what happens then, that the back button does go the previous page but it also directly logs me out of the profile after the dashboard page and when its on the login page the back button doesnt work the exit dialog box doesn't appear.
Please if someone can help me with this error .. here is the code for my back pressed button
public void onBackPressed() {
if (webview.isFocused() && webview.canGoBack() && webview==null) {
webview.goBack();
}
else {
new AlertDialog.Builder(this)
.setTitle("EXIT")
.setMessage("Are you sure. You want to close this app?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
I want when it reaches the dashboard it just asks for the exit dialog box when i press the back button twice that I created and then exit the app not directly the logged in id and doesnt got the login page directly.. I tried several times it doesnt work !
You need to change the condition in the if statement to:
if (webview.canGoBack() && webview.getUrl() != "URL of the login page") {
webview.goBack();
} else {
...
}
This will make the back button go back in the webview's history as long as the current URL is not the URL of the login page. When it reaches the login page, it will show the exit dialog.
Also, you have webview==null, this comparison will always return false and can be removed.

How do I trigger the Apptentive rating prompt reminder?

I just updated apptentive in my app to 1.5.0v. The rating prompt dialog is shown successfully when the conditions are true, but if the user clicks on "Remind me later", the rating prompt never is shown again.
I show the dialog, with the next code:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus
&& this instanceof SongActivity
&& InternalCache.getCounterApptentiveDialog() >= DOWNLOADS_TO_SHOW_APPTENTIVE) {
boolean ret = Apptentive.engage(this, "init");
if (ret) {
System.out.println("GA-APPtentive");
GAHelper.getInstance().apptentiveRateDialog(getClassName(),
getItemId());
}
}
}
Do I need something more in order to show the rating prompt dialog again?
You are reminded to rate the app again according to the value in your Apptentive Rating Prompt interaction's settings:
If it's set to 10 days, you will need to wait 10 days after pressing "Remind Me Later" to be re-prompted. You can simulate this by moving your device clock forward.
The "reminder" interaction will only be triggered if you engage its event. This event is the same as the main event used to trigger the Rating Prompt.
iOS:
[[ATConnect sharedConnection] engage:#"testRatingFlow" fromViewController:self];
Android:
Apptentive.engage(this, "testRatingFlow").

Why arent my log messages showing up?

I have some code that build a dialog box and makes a listener for it. The dialog box displays fine, but the code inside the listener doesn't seem to run, and I don't know why.
private void showBackgrounDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MeacruxActivity.this);
builder.setTitle(R.string.background_dialog_title).setCancelable(true)
.setItems(R.array.background_options,
new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int selection) {
Log.d(logID, "the selection is: " + selection);
if(backgrounds.length==selection){
notImplementedYet.show();
return;
}
setBckground(backgrounds[selection]);
}
});
currentDialog = builder.create();
currentDialog.show();
}
private void setBackground(String bgName) {
Log.d(logID, bgName);
}
The dialog shows up properly with all the options and everything, but when I click on one nothing come sup in the log.... Why is that?
Edit: I did some more testing and I can confirm that the code inside of the onClick function is being run, its just that the log isnt showing up...
I'm assuming you are looking in eclipse or studio
In DDMS view, make sure the device is selected.
In Logcat view, make sure there's not a filter applied.
On terminal, type adb logcat... does it show up there?

How to exit the app when user press the menu button on android phonegap

Hi Android Apps Developer around the world,
I am a newbie in android apps development. How to override menu button so that when user press the menu button, there will pop out and exit button for exitting the apps.
Thank you for your help.
Nobody seems to care you tagged cordova and html5 on your question! On your onDeviceReady event you set a listener to the menu button and call navigator.app.exitApp()
function onDeviceReady(){
document.addEventListener("menubutton", closeApp, false);
}
function closeApp(){
navigator.app.exitApp();
}
Be aware that while the other answers don't actually answer your question they have important information:
Not all devices have a menu button.
Be sure to notify the user you're closing the app, Adrien approach of showing a dialog is pretty standard and good enough.
If you try to publish an app that has a close button for iOS YOUR APP WILL PROBABLY BE REJECTED
Menu Button is deprecated! (since android 4)
Do not exit a App programmatically. Let the android system manage the apps lifecycle! There is no need to exit an app. Indeed you will destroy the Multitasking App context switching (which is the great feature of Android comparing to iOS). So the user can not go back to the current app state (screen / data etc.), because the app will start completely new, the user has to navigate to where he was previously etc.
Android's design does not favor exiting an application by choice, but rather manages it by the OS. You can bring up the Home application by its corresponding Intent:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_MENU)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to Exit?").setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
dialog.dismiss();
});
AlertDialog alert = builder.create();
alert.show();
}
return true;
}
Like this way :
EDIT
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// Create your alertDialog
new AlertDialog.Builder(this)
.setTitle("Exit ?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//Destroy your current activities
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}).create().show();
return true;
}
return super.onKeyUp(keyCode, event);
}
Hope this help

Overriding backbutton android phonegap 1.8 jqmobile 1.1.0 jquery 1.7.1

I recently started developing a mobile app in Phonegap 1.8 + Jquerymobile 1.1.0 +jquery 1.7.1 for Android 2.3.3. My problem is:
I've tried overriding backbutton in Android in several ways, nothing works though. I've tried using
document.addEventListener("backbutton", handleBackButton, false);
function handleBackButton() {//do sth}
then I've just overrided it in Application.java code. And it works... almost.
My 1st page (index.html) is being just used to login after that there is main page (main.html) with links to other pages. I've overrided backbutton to goback on every other page than login and main. On those two pages it should exit the app after clicking "yes" in AlertDialog. Well, in MainPage it shows the AlertDialog and then goes straight back to login page without waiting even for my response. On login page it just shows the AlertDialog and waits for my reaction. Don't know why. Here is some of my code.
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SampleApplication.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
String testUrl = appView.getUrl();
if(testUrl.equals("file:///android_asset/www/index.html"))
{
alert.show();
}
if (testUrl.equals("file:///android_asset/www/main.html"))
{
alert.show();
}
if ((!testUrl.equals("file:///android_asset/www/index.html")) && (!testUrl.equals("file:///android_asset/www/main.html")))
{
this.appView.goBack();
}
}
What you could do is use the regular back button on all pages that you want the regular back button behaviour but create a separate button for the pages where you need a different behaviour and just make it look like the normal back button:
In your header bar, just add an anchor where you add a custom data-rel (and hook it up to whatever function you want) e.g.
<a data-rel="custombackbtn">Back</a>
Then add in that data-rel to wherever your regular back button is styled (your theme's css or regular css) so, wherever there's a mention of a[data-rel='back'] add data-rel='custombackbtn' e.g.
a[data-rel='back'], .ui-header a[data-rel='back'] {
*/Styling*/
}
is changed to
a[data-rel='back'], .ui-header a[data-rel='back'], a[data-rel='backbtn'], .ui-header a[data-rel='backbtn'] {
*/Styling*/
}

Categories

Resources