gpt4 book ai didi

android - 单击 fragment 中的后退按钮后应用程序关闭

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:56:46 26 4
gpt4 key购买 nike

[更新]

问题已解决:只需在提交 fragment 之前添加“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();

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

我想返回上一个加载的 fragment 。

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

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

最佳答案

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

Add this transaction to the back stack. This means that the transaction will be remembered after it is committed, and will reverse its operation when later popped off the stack.

关于android - 单击 fragment 中的后退按钮后应用程序关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17792838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com