我有两个活动(A 和 B)。我在活动 A 中有一个片段 F。我的片段 F 包含 3 个 EditText (1,2,3)。
我的应用程序从 Activity B 获取一个字符串并将其放在 EditText 3 中。我对此没有任何问题。我的问题是,当我在 EditText 1 和 2 中输入内容时,我将从 ACTIVITY B 获取字符串并将其放入 EditText 3,我在 EditText 1 和 2 中输入的所有信息都消失了。
我的问题是,即使我将字符串从 ACTIVITY B 获取到 EditText 3,我在 EditText 1 和 2 中输入的信息将如何保留。这是我的代码:
活动一
public class ActivityA extends FragmentActivity {
ViewPager viewPager = null;
PageIndicator pIndicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
viewPager.setAdapter(new MyAdapter(fragmentManager));
pIndicator = (PageIndicator)findViewById(R.id.indicator);
pIndicator.setViewPager(viewPager);
}
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter (FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = null;
if (i == 0)
{
fragment = new FragmentF();
}
if (i == 1)
{
fragment = new FragmentG();
}
if (i == 2)
{
fragment = new FragmentH();
}
if (i == 3)
{
fragment = new FragmentI();
}
return fragment;
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
if (position == 0)
{
return "TAB.F";
}
if (position == 1)
{
return "TAB.G";
}
if (position == 2)
{
return "TAB.H";
}
if (position == 3)
{
return "TAB.I";
}
return null;
}
}
public void btn_to_actb(View view) {
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
}
活动乙
public final static String EXTRA_MESSAGE = "com.sample.MESSAGE";
// onClick get button from activity B layout
public void get(View view) {
Intent intent = new Intent(this, ActivityA.class);
TextView textView = (TextView)findViewById(R.id.coordinates);
String message = textView.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
片段 F
EditText editText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String num = getActivity().getIntent().getStringExtra("com.sample.MESSAGE");
View v = inflater.inflate(R.layout.fragmentf, container, false);
// EditText3 from fragment F layout
editText = (EditText) v.findViewById(R.id.edittext3);
editText.setText(num);
return v;
}