OnDetach/onAttach 片段重新创建片段活动
Posted
技术标签:
【中文标题】OnDetach/onAttach 片段重新创建片段活动【英文标题】:OnDetach/onAttach fragment recreate fragment activity 【发布时间】:2013-02-21 08:46:13 【问题描述】:我想分离/附加我的片段,但是如何设置,附加后该片段不会重新创建。
在片段中我有 WebView;当我选择和取消选择标签时,webview 加载起始页。
这是我的代码:
public class MainActivity extends Activity implements OnClickListener, OnMenuItemClickListener
ActionBar bar;
View v;
public static TextView tilt;
LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
onAddTab();
View v=getLayoutInflater().inflate(R.layout.action_bar, null);
ImageButton im = (ImageButton)v.findViewById(R.id.tab_toggle);
im.setOnClickListener(this);
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_layout));
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(v);
onToggleTabs();
@Override
public void onClick(View v)
// TODO Auto-generated method stub
switch (v.getId())
case R.id.tab_toggle:
onAddTab();
break;
public void onAddTab()
final ActionBar bar = getActionBar();
View v=getLayoutInflater().inflate(R.layout.layout_tab, null);
tilt = (TextView)v.findViewById(R.id.tit_le);
ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
v.setLayoutParams(lp);
closetab = (ImageButton)v.findViewById(R.id.close);
closetab.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
onRemoveTab();
);
bar.addTab(bar.newTab()
.setCustomView(v)
.setTabListener(new TabListener<Web>(this, "Tag A", Web.class)));
public void onRemoveTab()
final ActionBar bar = getActionBar();
Tab tab = bar.getSelectedTab();
bar.removeTab(tab);
public void onToggleTabs()
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
public void onRemoveAllTabs(View v)
getActionBar().removeAllTabs();
public static class TabListener<T extends Fragment> implements ActionBar.TabListener
private final Activity myActivity;
private final String myTag;
private final Class<T> myClass;
public TabListener(Activity activity, String tag, Class<T> cls)
myActivity = activity;
myTag = tag;
myClass = cls;
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
// Check if the fragment is already initialized
if (myFragment == null)
// If not, instantiate and add it to the activity
myFragment = Fragment.instantiate(myActivity, myClass.getName());
ft.add(R.id.fragment0, myFragment, myTag);
else
// If it exists, simply attach it in order to show it
ft.attach(myFragment);
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
if (myFragment != null)
// Detach the fragment, because another one is being attached
ft.detach(myFragment);
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
// TODO Auto-generated method stub
有片段:
public class Web extends Fragment implements OnLongClickListener, OnClickListener
@Override
public void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// TODO Auto-generated method stub
c=this.getActivity();
v = inflater.inflate(R.layout.activity_main, container, false);
return v;
@SuppressWarnings("deprecation")
@Override
public void onActivityCreated(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
wv = (WebView)v.findViewById(R.id.wv);
wv.setWebChromeClient(new WebChromeClient()
@Override
public void onProgressChanged(WebView view, int progress)
// TODO Auto-generated method stub
super.onProgressChanged(view, progress);
if(progress < 100 && pr.getVisibility() == ProgressBar.GONE)
pr.setVisibility(ProgressBar.VISIBLE);
pr.setProgress(progress);
if(progress == 100)
pr.setVisibility(ProgressBar.GONE);
);
wv.setWebViewClient(new MyWebViewClient());
wv.loadUrl("http://www.google.com");
wv.setOnLongClickListener(this);
【问题讨论】:
【参考方案1】:试试setRetainInstance(boolean)
还可以查看以下帖子: android fragments setRetainInstance(true) not works (Android support library) Fragment setRetainInstance not works (Android support lib) Understanding Fragment's setRetainInstance(boolean)
【讨论】:
我在 onActivityCreated 中设置了 RetainInstance(true),但还是同样的问题,Activity 重新创建。 “这只能与不在后台堆栈中的片段一起使用” - 你考虑过这个吗? 我的片段不在后栈中。 但是您写了“活动重新创建”-确实如此,然后没有创建片段。你确定你检查的方式正确吗?以上是关于OnDetach/onAttach 片段重新创建片段活动的主要内容,如果未能解决你的问题,请参考以下文章