简体   繁体   English

Android片段(带有WebView)-活动被破坏

[英]Android Fragments (with WebView) - Activity gets Destroyed

I am using a Tab based Activity in my App and I want to implement a Fragment Stack with WebViews. 我在我的应用程序中使用基于选项卡的活动,我想用WebViews实现一个片段堆栈。 In these WebViews I want to catch URL requests and replace the current WebView Fragment with a new one. 在这些WebView中,我想捕获URL请求并将当前的WebView Fragment替换为一个新的。 Unfortunately I always get Activity was destroyed Exception but don't know why. 不幸的是我总是得到Activity was destroyed Exception但是不知道为什么。

Can you please give me some hints how to fix this? 您能给我一些如何解决此问题的提示吗?

I use the following code (At the end of the code sample you will recognise the method handleUrlRequest where I want to replace the current Fragment): 我使用以下代码(在代码示例的最后,您将认识到要替换当前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.

    }
}

}

Finally I got it. 终于我明白了。 I passed the instance of Web through my WebViewClient where I handle the URL Request and want to do the Fragment Transaction. 我通过WebViewClient传递了Web实例,在该WebViewClient中我处理URL请求并希望执行片段事务。

There I call instanceOfWeb.getActivity().getSupportFragmentManager().beginTransaction() to init the Fragment Transaction. 在这里,我调用instanceOfWeb.getActivity().getSupportFragmentManager().beginTransaction()来启动片段事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当webview具有webviewclient时,活动被破坏 - Activity gets destroyed when webview has webviewclient 如果我的活动在显示另一个活动时被破坏了,我该如何处理持久化Android WebView的问题? - How should I deal with persisting an Android WebView if my activity gets destroyed while showing another one? 对活动的引用被销毁了? - Reference to Activity gets destroyed? Android片段-在片段中按文本后,“活动已被破坏” - Android Fragments - “Activity has been destroyed” after pressing text in a fragment 方向改变破坏了持有人的活动后,Android创建了额外的片段 - Android creates extra fragments after holder activity destroyed by orientation change 片段不会在活动结束时被销毁 - Fragments Not getting destroyed on Activity finish 重新创建()活动时不会破坏碎片 - Fragments not destroyed when recreate() activity Android:如何在Activity被销毁时保留AsyncTask实例? - Android: How to retain AsyncTask instance when Activity gets destroyed? 如果活动/应用程序在Android中被销毁,静态变量是否会保留? - Do static variables get preserve if activity/app gets destroyed in Android 活动暂停时是否应该销毁ViewPager片段? - Should a ViewPager fragments be destroyed when activity is paused?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM