Calling onKeyDown with Viewpager and Fragments - java

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.

Related

How to make WebView inside fragment to go back to previous tab?

I have a fragment which contains a webview.And the fragment is reused in all the four pages of a viewPager.What i want is, when the user clicks the back button i want the webView Fragment to go back to the previous tab of the current webViewFragment.And finally exit the activity.I can easily do this if the webView was inside an activity like this:
#Override
public void onBackPressed() {
if (mWebview.canGoBack())
mWebview.goBack();
else
super.onBackPressed();
}
But i cannot do this in a fragment because the fragment cannot override onBackPressed().How can i do this inside a fragment which is inside a viewPager
You can implement setOnKeyListener on webview to handle back button.
mWebview.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& mWebview.canGoBack())
{
mWebview.goBack();
return true;
}
return false;
}
});
If there is no previous page in your webview then it will trigger usual onBackPressed.
Hope it helps you.
This code is perfect for fragment webview backpress.
You can implement setOnKeyListener on webview to handle back button
mWebview.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& mWebview.canGoBack())
{
mWebview.goBack();
return true;
}
return false;
}
});

How to get hardkey events in a system overlay with a unfocusable view?

As I described in the Title, my problem is that there is a view that is not focusable. So now I've no idea how to get KeyEvents from the View...
Edit:
If possible, event can be listened from service
I have used this code in a fragment to close a full screen pager view and it works
View v = inflater.inflate(R.layout.fragment_addetails, container1,
false);
v.setFocusableInTouchMode(true);
v.requestFocus();
v.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& pager.getVisibility() == View.VISIBLE) {
pager.setVisibility(View.GONE);
return true;
}
return false;
}
});
or this
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
//do code
return true
}
return false;
}
maybe this will help
if not please describe the full scenario for what your are trying to achieve
Ok I found out, that I can use the MediaButton to get the KeyEvent I wanted. I just needed to declare a BroadcastReceiver and register it. Thats it!

I need to minimize the android application on back button click

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);

Executing simple code on android app before closing

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();
}

Android: How can I set a listener to the MenuButton?

I want to do a custom action when pressing on the Menu button on the phone.
Is it possible to set an onClickListener (or similar) on the button and if so, how?
onCreateOptionsMenu is only called the first time the button is pressed - I've already tried this.
Usually you shouldn't override MENU behavior as users expect menu to appear, however you can use something along these lines:
/* (non-Javadoc)
* #see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
*/
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
Log.d(TAG, "MENU pressed");
return true;
}
return super.onKeyDown(keyCode, event);
}
But onPrepareOptionsMenu(..) is called each time. :)
Updated for AppCompat v.22.+
As mentioned in this forum, KeyDown is not called for KEYCODE_MENU button pressed.
The solution is to override dispatchKeyEvent to this way:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
int action = event.getAction();
boolean isDown = action == KeyEvent.ACTION_DOWN;
if (keyCode == KeyEvent.KEYCODE_MENU) {
return isDown ? this.onKeyDown(keyCode, event) : this.onKeyUp(keyCode, event);
}
return super.dispatchKeyEvent(event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_MENU ) {
// do what you want to do here
return true;
}
return super.onKeyDown(keyCode, event);
}
It works until Google developers release a fix for this (or maybe it is not a bug and it works this way from now on).
You could probably hack something in using "OnMenuOpened" or some such, but I really wouldn't recommend it. The menu button is only supposed to be used to show menus, so there is consistency between applications.

Categories

Resources