我創建3個片段使用ViewPager,這是ViewPagerAdapter:Android的碎片無法更新
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
FragFile tab1 = new FragFile();
return tab1;
case 1:
FragAll tab2 = new FragAll();
return tab2;
case 2:
FragFav tab3 = new FragFav();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
這是活性1容納3片段:
public class Acc1 extends AppCompatActivity {
ViewPager view_pager;
TabLayout tab_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_acc1);
tab_layout = (TabLayout) findViewById(R.id.tab_layout);
tab_layout.addTab(tab_layout.newTab().setText("File"));
tab_layout.addTab(tab_layout.newTab().setText("All"));
tab_layout.addTab(tab_layout.newTab().setText("Fav"));
view_pager = (ViewPager) findViewById(R.id.viewpager);
final ViewPagerAdapter adapter = new ViewPagerAdapter
(getSupportFragmentManager(), tab_layout.getTabCount());
view_pager.setAdapter(adapter);
view_pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));
tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
view_pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
在MainActivity中,轉到Activity1保存3 Fragment。
在Activity1中,Fragment2(FragAll)和Fragment3(FragFav)可以轉到Acctivity2。
在Activity2中可以編輯數據,這個數據會顯示在Fragment3(FragFav)上。
我的問題是:
當活性2編輯數據,然後回來活性1使用backpress,在Fragment3數據(FragFav)不會改變(我認爲應用程序保持碎片狀態)。 (如果從Activity2返回Activity1,選擇Fragment1(FragFile)並選擇Fragment3(FragFav),數據也是如此),但是當離開Acitivity1時使用backpress到MainActivity並再次轉到Activity1,Fragment3(FragFav)將改變)
我知道我需要刷新或重置Fragment3(FragFav)時,從Activity2復出到Activity1,但我不知道如何去。
任何人都可以幫助我嗎?
這是更新我跟着DRV:
我在java文件夾創建AppConstants類:
public class AppConstants {
public static boolean refreshPage=false;
public int fragment_position=0;
}
在活動1,更改代碼:
public class Acc1 extends AppCompatActivity {
ViewPager view_pager;
TabLayout tab_layout;
AppConstants ac;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_acc1);
tab_layout = (TabLayout) findViewById(R.id.tab_layout);
tab_layout.addTab(tab_layout.newTab().setText("File"));
tab_layout.addTab(tab_layout.newTab().setText("All"));
tab_layout.addTab(tab_layout.newTab().setText("Fav"));
view_pager = (ViewPager) findViewById(R.id.viewpager);
final ViewPagerAdapter adapter = new ViewPagerAdapter
(getSupportFragmentManager(), tab_layout.getTabCount());
view_pager.setAdapter(adapter);
view_pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));
tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
view_pager.setCurrentItem(ac.fragment_position);
ac.fragment_position=tab.getPosition();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
@Override
public void onResume() {
super.onResume();
if (AppConstants.refreshPage) {
AppConstants.refreshPage = false;
finish();
startActivity(getIntent());
}
}
在Activity2中,我添加了以下代碼:
@Override
public void onBackPressed() {
Intent setIntent = new Intent(Acc2.this, Acc1.class);
AppConstants.refreshPage=true;
startActivity(setIntent);
}
但是當選擇Fragment2(FragAll)和Fragment3(FragFile)時,我在「view_pager.setCurrentItem(ac.fragment_position);」
我跟着你,但是當從活動1翻盤使用backpress的活性2,它不顯示舊片段,它顯示片段1( FragFile) – 1234abcd
@ 1234abcd檢查我的答案,你會得到你想要的。 – Drv
什麼是fragment_position? – 1234abcd