简体   繁体   English

重点关注WebView时未触发Android webview onBackPressed

[英]Android webview onBackPressed not fired when WebView is focused

I have an activity, with a webView(Chromium webView https://github.com/mogoweb/chromium_webview/ ) inside it. 我有一个活动,里面有一个webView(Chromium webView https://github.com/mogoweb/chromium_webview/ )。

The screen has two parts, top part is a ViewPager and bottom is a WebView. 屏幕分为两部分,顶部是ViewPager,底部是WebView。 When the activity is loaded and webView is not in focus, back key works as expected and onBackPressed and onKeyDown are fired. 加载活动且未集中webView时,后退键将按预期工作,并且会触发onBackPressed和onKeyDown。

However, the moment webView receives focus(by tapping or focusing a textfield), the activity doesn't seem to treat back key as expected and both onBackPress and onKeyDown are not fired. 但是,当webView获得焦点(通过点按或聚焦文本字段)时,活动似乎并未按预期方式对待后退键,并且不会触发onBackPress和onKeyDown。

Things I have tried: - I have tried both onBackPress and onKeyDown. 我尝试过的事情:-我同时尝试了onBackPress和onKeyDown。 - Tried setting android:descendantFocusability="blocksDescendants" for the linearlayout, but webView doesn't receive focus anymore. -尝试将android:descendantFocusability =“ blocksDescendants”设置为linearlayout,但是webView不再获得焦点。

I believe most of the code is standard java and since the events fire when webView is not in focus, I assume there is something I am not handling well when it comes to focusing a webView. 我相信大多数代码都是标准的Java,并且由于当webView不在焦点时会触发该事件,因此我认为在聚焦webView时我不能很好地处理某些事情。

The activity is a "Fragment Activity" 该活动是“片段活动”

Any suggestions are highly appreciated.? 任何建议都将受到高度赞赏。 Following is the code for backpress and keydown, and even the debug messages are not to be seen and no errors as well. 以下是backpress和keydown的代码,甚至看不到调试消息,也没有错误。

XML: XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/order_mode"
    android:descendantFocusability="blocksDescendants">
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 

        >           
        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/frame_surface_view"
            >
        <io.vov.vitamio.widget.VideoView
            android:id="@+id/surface_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:layout_gravity="top" 
            />
        </LinearLayout>
        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/frame_surface_view"
            android:layout_alignRight="@+id/frame_surface_view"  
            android:layout_margin="20dp"      
            >
        <ImageButton
            android:id="@+id/btnGift"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/gift" 
            />
        <ImageButton
            android:id="@+id/btnPraise"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/praise" 
            android:layout_marginLeft="15dp"

            />      
        <Spinner
            android:id="@+id/spinnerPriaseMessages"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:textColor="#000000"
            android:gravity="center_horizontal|center_vertical"
            android:textSize="12sp"
            android:layout_below="@+id/indicator"
            android:layout_toRightOf="@+id/txtCount"
            android:background="@drawable/bg_input"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:entries="@array/praise_count_array"
            android:prompt="@string/selectPraise"
            android:spinnerMode="dialog" />                 
        </LinearLayout>
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/webViewRelative"
        android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <com.mogoweb.chrome.WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="true" />    
    </RelativeLayout> 
    <io.vov.vitamio.widget.CenterLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </io.vov.vitamio.widget.CenterLayout>

</LinearLayout>

The events 事件

@Override
public void onBackPressed(){
    System.out.println("Back Pressed");
    if(webView.isFocused()){
        super.onBackPressed();
        finish();   
    }
    System.out.println("BackPressed");
    if(!giftShown){
        mVideoView.stopPlayback();

        finish();                   
    }else{
        System.out.println("Hiding Fragment Gift");
        getSupportFragmentManager().popBackStack();
        giftShown=false;
    }           

}    
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    System.out.println("onKeyDown");
    if ((keyCode == KeyEvent.KEYCODE_BACK) ) {

        if(webView.isFocused()){
            super.onBackPressed();
            finish();
        }
        System.out.println("BackPressed");
        if(!giftShown){
            mVideoView.stopPlayback();
            finish();
        }else{
            System.out.println("Hiding Fragment Gift");
            getSupportFragmentManager().popBackStack();
            giftShown=false;
        }
    }
    return super.onKeyDown(keyCode, event);
}

I tried this way to solved this problem, add this code on your onCreateView fragment : 我尝试过这种方法来解决此问题,在您的onCreateView片段上添加以下代码:

webView.setOnKeyListener(new OnKeyListener(){
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
                    webView.clearFocus();
                    return true;
                }
                return false;
            }
        });

OR try this way on your fragmentActivity : 或者在您的fragmentActivity上尝试这种方式:

 if (webView.isFocused() && webView.canGoBack()) {
    super.onBackPressed();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM