Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 进入上一视图或将应用程序保留为onKeydown_Android_View_Onkeydown - Fatal编程技术网

Android 进入上一视图或将应用程序保留为onKeydown

Android 进入上一视图或将应用程序保留为onKeydown,android,view,onkeydown,Android,View,Onkeydown,我有一个浏览首页 第二个视图是webview 我想做的是,当用户在Webview上时,如果他按back键,他将返回frontpageview,如果他在frontpageview上,应用程序将关闭 我试过这样的方法: @Override public boolean onKeyDown(int keyCode, KeyEvent event){ if (event.getKeyCode() == KeyEvent.KEYCODE_B

我有一个浏览首页 第二个视图是webview 我想做的是,当用户在Webview上时,如果他按back键,他将返回frontpageview,如果他在frontpageview上,应用程序将关闭

我试过这样的方法:

            @Override
        public boolean onKeyDown(int keyCode, KeyEvent event){

            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                if(findViewById(R.id.frontpage_view).getTag().equals("front")) 
                    finish();
                else if(findViewById(R.id.webView_web_app).getTag().equals("webv")) 
                    setContentView(R.layout.frontpage);}
                return true;}}
以下是frontpage开头的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
   android:id="@+id/frontpage_view"
android:tag="front"
 >
和网络视图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WebAppActivity"
 >

<WebView
    android:id="@+id/webView_web_app"
    android:tag="webv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" />
当布局为frontpage时,当按下后退键时,它将离开应用程序。 但一旦我进入webview布局,当我按下backkey时,应用程序就会崩溃。
我做得对吗?

使用活动和框架管理您的视图。了解