Android 在片段中单击后退按钮后,应用程序关闭
[更新] 问题已解决:只需在提交片段之前添加“addToBackStack(null)”:Android 在片段中单击后退按钮后,应用程序关闭,android,android-fragments,Android,Android Fragments,[更新] 问题已解决:只需在提交片段之前添加“addToBackStack(null)”: Fragment fragment = new WebBrowserFragment(); fragment.setArguments(arguments); FragmentTransaction fragmentTransaction = getActivity().getFragmentManager().beginTransaction(); fragmentTransaction.replace
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;
}
公共类WebBrowserFragment扩展了片段{
网络视图;
ProgressBar progressB=null;
私人字符串mUrl;
私有静态字符串mtitle;
私有静态字符串msource;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
LayoutFlater mInflater=(LayoutFlater)getActivity().getSystemService(Activity.LAYOUT\u INFLATER\u SERVICE);
视图=(视图)mInflater.充气(R.layout.web\u浏览器\u视图,空);
MainActivity.setShareButtonVisible();
Bundle=getArguments();
字符串url=bundle.getString(“url”);
mtitle=bundle.getString(“TITRE”);
msource=bundle.getString(“源”);
mUrl=url;
progressB=(ProgressBar)view.findViewById(R.id.progressBar1);
mWebView=(WebView)view.findViewById(R.id.webViewArticle);
mWebView.setWebChromeClient(新WebChromeClient(){
public void onProgressChanged(WebView视图,int-progress){
如果(进度<100&&progressB.getVisibility()==ProgressBar.GONE){
progressB.setVisibility(ProgressBar.VISIBLE);
}
进度b.设置进度(进度);
如果(进度==100){
progressB.setVisibility(ProgressBar.GONE);
}
}
});
加载url(url);
返回视图;
}
将片段附加到活动时,必须使用FragmentTransaction.addToBackStack
方法。以下是文档中的引用
将此事务添加到后堆栈。这意味着事务提交后将被记住,并在稍后从堆栈弹出时反转其操作
我必须在
fragmentManager.beginTransaction().replace(R.id.content_frame,fragment.commit();
之前调用它?