我使用ViewPager
在Fragments
上创建3 Activity_1
,这是ViewPagerAdapter
:
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:
Fragment1 tab1 = new Fragment1();
return tab1;
case 1:
Fragment2 tab2 = new Fragment2();
return tab2;
case 2:
Fragment3 tab3 = new Fragment3();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
这是Activity_1
,其中包含所有Fragments
,并在Fragment_1
打开时自动显示Activity_1
:
public class Activity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
TabLayout tab_layout = (TabLayout) findViewById(R.id.tab_layout);
tab_layout.addTab(tab_layout.newTab().setText("Frag1"));
tab_layout.addTab(tab_layout.newTab().setText("Frag2"));
tab_layout.addTab(tab_layout.newTab().setText("Frag3"));
final ViewPager 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) {
}
});
}
每个Fragment
都可以转到Activity_2
Fragment_3
包含可在Activity_2
上编辑的数据。
我的问题:
使用后退按钮从Activity_2
编辑数据并返回Activity_1
时,
Fragment_3
中的数据仍然是旧的,未使用来自Activity_2
的已编辑数据进行更改
但是,当关闭Activity_1
并再次打开时,Fragment_3
中的数据会发生变化。
当我从Fragment_3
回到Activity_1
时,我想我需要重置或重新加载Activity_2
,但我不知道如何。
更新,
这是Activity_2:
public class Activity2 extends AppCompatActivity {
TextView tv_id;
String id;
TextView tv_d;
Button bt_add, bt_remove;
String str_find;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Intent i = getIntent();
id = i.getStringExtra("id");
tv_id = (TextView)findViewById(R.id.tv_DetailId);
tv_id.setText(id);
tv_d = (TextView)findViewById(R.id.tv_Detail);
bt_add = (Button)findViewById(R.id.bt_detail_add);
bt_remove = (Button)findViewById(R.id.bt_detail_remove);
str_find = "example";
try {
final File file = new File(Environment.getExternalStorageDirectory() + "/TestFolder/Test.txt");
StringBuilder text = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
xml_root = text.toString();
FileInputStream in = new FileInputStream(file);
int len = 0;
byte[] data1 = new byte[1024];
while ( -1 != (len = in.read(data1)) ){
if(new String(data1, 0, len).contains(str_find)){
bt_add.setVisibility(View.GONE);
tv_d.setText("yes");
bt_remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//something to edit data on Test.txt
}
});
}
else {
bt_remove.setVisibility(View.GONE);
tv_d.setText("no");
bt_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//something to edit data on Test.txt
}
});
}
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
在onResume()
方法中,您可以进行条件检查
view_pager.setCurrentItem(last_frag);
其中last_frag
是一个全局整数,每次在页面更改侦听器中设置它的值。
这会刷新你的片段。
在您的Activity1.java
中进行以下更改
private ViewPager view_pager;
将是您的全局变量
并在初始化期间删除final ViewPager
。
在onCreate()
方法
view_pager = (ViewPager) findViewById(R.id.viewpager);
private int last_frag = 0;
将成为Activity1.java
@Override
public void onTabSelected(TabLayout.Tab tab) {
view_pager.setCurrentItem(tab.getPosition());
last_frag = tab.getPosition();
}