Android 进入上一视图或将应用程序保留为onKeydown
我有一个浏览首页 第二个视图是webview 我想做的是,当用户在Webview上时,如果他按back键,他将返回frontpageview,如果他在frontpageview上,应用程序将关闭 我试过这样的方法: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
@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时,应用程序就会崩溃。
我做得对吗?使用活动和框架管理您的视图。了解