Hello, I've been trying to integrate airpush in andriod but I've been having some issues.
#Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
As you can see is the andriod go back code for webview via history.
The problem is that I want execute this code when it has no more history, meaning when the application is about to close.
airpush.startLandingPageAd();
Any help will be greatly appreciated guys :)..
You need to edit your code, like this:
#Override public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if(mWebView.canGoBack())
{
mWebView.goBack();
return true;
}
else
{
//Your webview doesn't have any history here and you can close your app here.
//You can also execute your Airpush code here.
airpush.startLandingPageAd();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
I hope this helps.
Consider overriding onPause method of Activity for executing a piece of code before the activity hides.
#Override
protected void onPause() {
super.onPause();
airpush.startLandingPageAd();
}
Related
I want to detect when the power button has been pressed (not long press) in my app.
I'm using the following code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Log.d("mytag", "keycode_power");
return true;
}
return super.onKeyDown(keyCode, event);
}
However, nothing prints to log. I've also tried:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
Log.d("mytag", "power off");
}
return super.dispatchKeyEvent(event);
}
but that doesn't work either.
Any suggestions?
I'm No Expert, But I Don't Think That The Power Button Counts As A Keypress
I need to minimize the application when back button is pressed.
I use following code to catch hardware back button click event
help me with the code of minimize on back key pressed
#Override
public boolean onKeyDown(int keyCode, keyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_BACK;
//minimize application
return true;
}
return super.onKeyDown(keyCode, event);
}
I think that you need to treat back event as home event. The code below is how I emulate home pressed when User press back button:
public void minimizeApp() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
This is a simple code to minimize the application
#Override
public void onBackPressed() {
this.moveTaskToBack(true);
}
try this code, this will minimize Activity.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
this.moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
or
If you want to close the activity use this.finish() method to close the current running activity. instead of this.moveTaskToBack(true);
How can i make the below code work with a viewpagers that is loading 3 fragments, all are webviews with different URL's. Im simply trying to make it so that when clicking back on whatever fragment, it calls web.goBack();
Here is the code i've used in normal activities, I guess i need recommendations on weather this code should be inside the indivisual fragments or inside the main viewpager. Any suggestions or examples, or code would be greatly appreciated.
public boolean onKeyDown(int KeyCode, KeyEvent event) {
if ((KeyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(KeyCode, event);
}
You can override this in main Activity and then in on onKeyDown(int KeyCode, KeyEvent event) get your ViewPagers current fragment. In the end you will able to do something like this :
public boolean onKeyDown(int KeyCode, KeyEvent event) {
//.. get your fragment
WebView web = fragment.getWeb();
if ((KeyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(KeyCode, event);
}
Best wishes.
Does anybody know how to shield the HOME key in Android 4.0?
The code below only works in 2.2 and 2.3, so how would I change this so it can work in 4.0?
/* FIXME: How does it works within Android 4.0? */
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
Try this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
//do nothing
return true;
}
return super.onKeyDown(keyCode, event);
}
It's not an edit to your code, but it's an alternative work around for the same purpose.
why my back button doesn't function in google map activity ?
already include:
#Override
public void onBackPressed() {return;}
but still doesn't work.
You need this:
#Override
public void onBackPressed() {
super.onBackPressed();
}
Or this pre API level 5:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
// your implementation here
// finish(); - to exit the Activity
return true; // shows you consumed the event with your implementation
}
return super.onKeyDown(keyCode, event);
}
write
super.onBackPressed();
or
this.finish();
instead of return;