[英]Android Fragments (with WebView) - Activity gets Destroyed
我在我的應用程序中使用基於選項卡的活動,我想用WebViews實現一個片段堆棧。 在這些WebView中,我想捕獲URL請求並將當前的WebView Fragment替換為一個新的。 不幸的是我總是得到Activity was destroyed Exception
但是不知道為什么。
您能給我一些如何解決此問題的提示嗎?
我使用以下代碼(在代碼示例的最后,您將認識到要替換當前Fragment的方法handleUrlRequest
):
public class FragmentStackSupport extends SherlockFragmentActivity {
int mStackLevel = 1;
public static int THEME = R.style.Theme_Sherlock_Light;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(THEME); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_stack);
if (savedInstanceState == null) {
// Do first time initialization -- add initial fragment.
Fragment newFragment = Web.newInstance(mStackLevel);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.simple_fragment, newFragment).commit();
} else {
mStackLevel = savedInstanceState.getInt("level");
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("level", mStackLevel);
}
public void addFragmentToStack() {
mStackLevel++;
// Instantiate a new fragment.
Fragment newFragment = Web.newInstance(mStackLevel);
// Add the fragment to the activity, pushing this transaction
// on to the back stack.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
public static class Web extends SherlockFragment {
int mNum;
/**
* Create a new instance of CountingFragment, providing "num"
* as an argument.
*/
static Web newInstance(int num) {
Web f = new Web();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
/**
* The Fragment's UI is just a simple text view showing its
* instance number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello_world, container, false);
View wv = v.findViewById(R.id.webview);
WebView webView = ((WebView)wv);
webView.loadUrl("http://www.google.at");
webView.setWebViewClient(new MyWebViewClient());
return v;
}
}
public static class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlString) {
handleURLRequest("modal");
return false;
}
private void handleURLRequest(String transitionStyle){
// here I want to replace the Fragment with a new one.
}
}
}
終於我明白了。 我通過WebViewClient傳遞了Web實例,在該WebViewClient中我處理URL請求並希望執行片段事務。
在這里,我調用instanceOfWeb.getActivity().getSupportFragmentManager().beginTransaction()
來啟動片段事務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.