【问题标题】:App closed after click on back button in Fragment单击片段中的后退按钮后应用程序关闭
【发布时间】:2013-07-23 00:57:10
【问题描述】:

[更新]

问题已解决:只需在提交片段前添加“addToBackStack(null)”即可:

Fragment fragment = new WebBrowserFragment();
fragment.setArguments(arguments);
FragmentTransaction fragmentTransaction = getActivity().getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

这是我的片段的代码。当我在这个片段上并单击后退按钮时,我的应用程序已关闭。

我想回到之前加载的片段。

我能做些什么来强制这种行为?

public class WebBrowserFragment extends Fragment {

    WebView mWebView;
    ProgressBar progressB = null;
    private String mUrl;
    private static String mtitle;
    private static String msource;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LayoutInflater mInflater = (LayoutInflater) getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view =  (View) mInflater.inflate(R.layout.web_browser_view, null);

        MainActivity.setShareButtonToVisible();

        Bundle bundle = getArguments(); 
        String url = bundle.getString("URL");
        mtitle = bundle.getString("TITRE");
        msource = bundle.getString("SOURCE");

        mUrl = url;

        progressB = (ProgressBar) view.findViewById(R.id.progressBar1);

        mWebView = (WebView) view.findViewById(R.id.webViewArticle);
        mWebView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if(progress < 100 && progressB.getVisibility() == ProgressBar.GONE){
                    progressB.setVisibility(ProgressBar.VISIBLE);
                }
                progressB.setProgress(progress);
                if(progress == 100) {
                    progressB.setVisibility(ProgressBar.GONE);
                }
            }
        });
        mWebView.loadUrl(url);  
        return view;
    }

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    在将片段附加到 Activity 时,您必须使用 FragmentTransaction.addToBackStack 方法。这是文档中的引用

    将此事务添加到后台堆栈。这意味着事务将在提交后被记住,并在以后从堆栈中弹出时反转其操作。

    【讨论】:

    • 我必须在fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();之前调用它?
    猜你喜欢
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多