【发布时间】:2017-12-22 19:18:50
【问题描述】:
我使用DialogFragment 包含一个webview。显示DialogFragment 后,当按下返回按钮时,应用程序将返回活动视图。如果按下显示 DialogFragment 按钮,webview 将再次加载 init url。但我的要求是,如果第二次打开 DialogFragment ,我希望上次加载 webview 显示页面。我怎样才能做到这一点?
我目前的想法是保留 DialogFragment 实例,当按下显示按钮然后检查活动是否已经有 DialogFragment。但是在我的代码中,findFragmentByTag 总是返回 null ,有什么问题?还是我应该在 DialogFragment 中做一些工作?
主要活动代码:
mcWebViewDialog m_webviewDialog = null;
public void showWebviewDialog()
{
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
Fragment prev=fragmentManager.findFragmentByTag("mcWebDialog");
if(prev!=null)
{
Log.v("seayoung","m_webviewDialog not null");
fragmentTransaction.show(prev);
}
else
{
Log.v("seayoung","m_webviewDialog null");
m_webviewDialog=mcWebViewDialog.newInstance();
m_webviewDialog.set_loadurl("file:///android_asset/input.html");
m_webviewDialog.show(fragmentManager,"mcWebDialog");
}
}
WebViewDialog 代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
//other code
m_webView.loadUrl(m_loadurl);
return m_webView;
}
【问题讨论】:
标签: android android-fragments android-webview android-dialogfragment