![](/img/trans.png)
[英]Music in Webview Activity kill when press back button to home activity
[英]Prevent Reload Activity / Webview when go Back to app and when press the turn off button
我在Activity中有一個片段,然后在該片段中也有一個webview。
問題 :鎖定屏幕並按關閉按鈕解鎖后,將重新創建我的活動或重新加載Web視圖(不確定會發生什么)。
當我切換設備中的應用程序時,會發生相同的問題。
我試圖更改configChanges和launchMode。 這是我在android清單文件中的活動的一部分:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize"
android:launchMode="singleTop"
>
</activity>
我也嘗試在myCreate上檢查我的savedInstanceState:
@Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
this.init(); // Initialize my webview
}
...
編輯:
@Override
protected void onResume() {
super.onResume();
//Check if the user is authenticated onResume
String message = getIntent().getStringExtra(MainActivity.FragmentIdentifier);
if(message == null || message.compareTo(MainActivity.showWebViewFragment) == 0){
changeFragment(new WebViewFragment());
}else if (message.compareTo(MainActivity.showLoginFragment) == 0){
changeFragment(new LoginFragment());
}
}
任何想法或代碼示例將不勝感激,謝謝!
我發現我應該處理實例並檢查是否已經保存,否則我的onResume總是可以重新加載我的webview。 這是我的新代碼:
@Override
protected void onResume() {
super.onResume();
//Check if the user is authenticated onResume
String message = getIntent().getStringExtra(MainActivity.FragmentIdentifier);
if (message == null || message.compareTo(MainActivity.showWebViewFragment) == 0) {
/*
Check if the instance is saved. If yes, is not necessary to create a new instance of the webview,
otherwise will always reload.
*/
if (!getSavedInstance()){
changeFragment(new WebViewFragment());
}
} else if (message.compareTo(MainActivity.showLoginFragment) == 0) {
changeFragment(new LoginFragment());
}
}
我聲明了一個新的全局變量
private Boolean savedInstance = false;
這是我的設定
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setSavedInstance(true);
}
public Boolean getSavedInstance() {
return savedInstance;
}
public void setSavedInstance(Boolean instance_saved) {
this.savedInstance = instance_saved;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.