Я впервые создаю приложение для веб-просмотра с использованием Android Studio. Так что у меня есть веб-сайт, который содержит страницы входа. Мне нужно разрабатывать приложения с помощью веб-просмотра Проблема заключалась в том, что когда приложение было недавно установлено, затем открывало его, затем вкладку смены пользователя, откат веб-просмотра до его основного URL-адреса веб-просмотра. Но после этого он больше не беспокоится о своей полной работоспособности. Я хочу удалить это, чтобы пользователь мог менять вкладку на первом месте, не разрушая его текущие действия. Это коды основной деятельности. Я не использую фрагменты.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Context mContext;
private Activity mActivity;
private CoordinatorLayout mCLayout;
private WebView mWebView;
boolean isHomePage = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the application context for back button and exit
mContext = getApplicationContext();
mActivity = MainActivity.this;
mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
mWebView = (WebView) findViewById(R.id.webview);
// Zoom out of the website to mobile view.
WebView browser = (WebView) findViewById(R.id.webview);
browser.getSettings().setLoadWithOverviewMode(true);
browser.getSettings().setUseWideViewPort(true);
// >>> Enable zoom IN and OUT.
browser.getSettings().setBuiltInZoomControls(true);
browser.getSettings().setDisplayZoomControls(false);
// Enabling Javascript.
final WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Loading the URL to Webview.
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mWebView.loadUrl("url");
// Enabling DOM Storage most IMPORTANT one
mWebView.getSettings().setDomStorageEnabled(true);
// Enabling Automatic Javascript when opening windows not needed just to make sure
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
// Preventing to load on browser - it keeps loading to the same webview even the link is targeting to other.
myWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
viewx.loadUrl(urlx);
return false;
}
});
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
if (url.contains("url")) {
isHomePage = true;
return true;
} else
isHomePage = false;
return false;
}
}
);
// Hiding Status and Dock bar when opening the applications
View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
}
protected void showAppExitDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder.setTitle("Please confirm");
builder.setMessage("Do you want to exit the app?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Do something when user want to exit the app
// Let allow the system to handle the event, such as exit the app
MainActivity.super.onBackPressed();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Do something when want to stay in the app
Toast.makeText(mContext, "thank you", Toast.LENGTH_LONG).show();
}
});
// Create the alert dialog using alert dialog builder
AlertDialog dialog = builder.create();
// Finally, display the dialog when user press back button
dialog.show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
String url = new String("url");
String webUrl = new String(mWebView.getUrl());
if (url.equals(webUrl)) {
Toast.makeText(this, "You are logged in. Please Logout if you want to exit.",
Toast.LENGTH_SHORT).show();
} else if (isHomePage == false) {
if (mWebView.canGoBack()) {
Toast.makeText(this, "DoubleBack",
Toast.LENGTH_SHORT).show();
mWebView.goBack();
mWebView.goBack();
isHomePage = true;
}
}else{
url = new String("url");
webUrl = new String(mWebView.getUrl());
if (url.equals(webUrl)) {
Toast.makeText(this, "Access Denied",
Toast.LENGTH_SHORT).show();
}
else
if (mWebView.canGoBack()) {
Toast.makeText(this, "Back",
Toast.LENGTH_SHORT);
mWebView.goBack();
}
}
return true;
default:
}
}
return super.onKeyDown(keyCode, event);
}
}
Это мой AndroidManifest. xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.test"
android:installLocation="auto"
android:versionCode="3210"
android:versionName="1.1.0"
>
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:targetSdkVersion="10"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
android:isGame="false"
>
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:windowSoftInputMode="adjustNothing|adjustPan"
android:label="@string/app_name"> <!--Prevent Reloading when changing screen rotation-->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>