Prevent WebView from reloading the website after orientation change in WebViewActivity.

This commit is contained in:
Docile-Alligator 2022-03-17 21:22:26 +08:00
parent b4915859da
commit 59ed4c53ff
2 changed files with 19 additions and 3 deletions

View File

@ -76,7 +76,8 @@
android:name=".activities.WebViewActivity" android:name=".activities.WebViewActivity"
android:label="" android:label=""
android:parentActivityName=".activities.MainActivity" android:parentActivityName=".activities.MainActivity"
android:theme="@style/AppTheme.NoActionBar" /> android:theme="@style/AppTheme.NoActionBar"
android:configChanges="orientation|screenSize" />
<activity <activity
android:name=".activities.SuicidePreventionActivity" android:name=".activities.SuicidePreventionActivity"
android:parentActivityName=".activities.MainActivity" android:parentActivityName=".activities.MainActivity"

View File

@ -19,6 +19,7 @@ import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
@ -79,8 +80,10 @@ public class WebViewActivity extends BaseActivity {
webView.getSettings().setDomStorageEnabled(true); webView.getSettings().setDomStorageEnabled(true);
url = getIntent().getDataString(); url = getIntent().getDataString();
toolbar.setTitle(url); if (savedInstanceState == null) {
webView.loadUrl(url); toolbar.setTitle(url);
webView.loadUrl(url);
}
WebViewClient client = new WebViewClient() { WebViewClient client = new WebViewClient() {
@Override @Override
@ -177,4 +180,16 @@ public class WebViewActivity extends BaseActivity {
} }
return super.onKeyDown(keyCode, event); return super.onKeyDown(keyCode, event);
} }
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
webView.restoreState(savedInstanceState);
}
} }