eclipse 按下后退按钮时重新启动片段类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33461560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
restart fragment class when back button press
提问by user5508330
I have a fragment that is part of tab view. I want to restart this fragment when I press back button. but i don't know how to refresh it. i tried some codes like this: Restart fragment inside activitybut it didn't work.
我有一个片段,它是选项卡视图的一部分。当我按下后退按钮时,我想重新启动这个片段。但我不知道如何刷新它。我尝试了一些这样的代码: 在活动中重新启动片段但它没有用。
here is a code for detecting back button:
这是用于检测后退按钮的代码:
@Override
public void onResume() {
super.onResume();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
// handle back button
return true;
}
return false;
}
});
}
here is a fragment class button:
这是一个片段类按钮:
public class MoviesFragment extends Fragment {
static ListView list;
static String[] numbers_text = new String[] { "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen" };
String[] songs = new String[] { "1", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "11", "12", "13", "14", "15" };
MainCustomList adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_movies, container, false);
adapter = new MainCustomList(this.getActivity(), numbers_text);
list=(ListView) view.findViewById(R.id.list);
list.setAdapter(adapter);
return view;
}
main:
主要的:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
回答by Mohammed Aouf Zouag
Use this code:
使用此代码:
YourFragmentClass fragment = (YourFragmentClass)
getFragmentManager().findFragmentById(R.id.your_fragment_container_id)
getFragmentManager().beginTransaction()
.detach(fragment)
.attach(fragment)
.commit();
& this will cause your fragment
's OnCreateView
method to be calledagain & refresh its layout.
& 这将导致您fragment
的OnCreateView
方法再次被调用并刷新其布局。
UPDATE:
更新:
When using the Support Library
, you could access your fragment this way:
使用 时Support Library
,您可以通过以下方式访问您的片段:
int YOUR_FRAGMENT_POSITION = 2; // in your case you wanna access the 3rd fragment, 0 indexed
YourFragmentClass fragment = (YourFragmentClass) getSupportFragmentManager().getFragments().get(YOUR_FRAGMENT_POSITION);
And use the same code snippet above:
并使用上面相同的代码片段:
getSupportFragmentManager().beginTransaction()
.detach(fragment)
.attach(fragment)
.commit();
If you try to access your fragment
via the PagerAdapter
, you may end up with an IllegalStateException
, telling you that the fragment has failed to save its state
.
如果您尝试通过 访问您fragment
的PagerAdapter
,您可能会以 结束IllegalStateException
,告诉您片段未能保存其state
.