应用点击后关闭了后退按钮在片段

问题描述:

[更新]应用点击后关闭了后退按钮在片段

问题解决了:只需添加“addToBackStack(空)”之前承诺的片段:

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; 
    } 
+0

发布logcat。 –

你有你的连接片段到活动时要使用FragmentTransaction.addToBackStack方法。以下是来自文档的报价

将此事务添加到反向堆栈。这意味着事务在被执行后将被记住,并且在稍后弹出堆栈时将反转其操作。

+0

我必须在'fragmentManager.beginTransaction()之前调用它。replace(R.id.content_frame,fragment).commit();'? – wawanopoulos

+1

解决!看我的帖子 – wawanopoulos

+0

是的,这是正确的:) – Desert