简体   繁体   中英

Resuming state of fragment activity

I have two ACTIVITY (A and B). I have a FRAGMENT F in ACTIVITY A. My Fragment F cointains 3 EditText (1,2,3).

My app gets a string from Activity B and places it in EditText 3. I have no problem with that. My problem is, when i type something in EditText 1 and 2 then I'll get the string from ACTIVITY B and put it in EditText 3, all of the information I typed in EditText 1 and 2 are gone.

My question is, how will the information I typed in EditText 1 and 2 stay even though I get the string from ACTIVITY B to EditText 3. Here's my code:

ACTIVITY A

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);
    }

ACTIVITY B

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);     
    }

FRAGMENT 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;
    }

Notice that when you launch your intent to Activity A from B, the Activity A could be recreated o resumed depending on if A was stopped or if it was still on memory.

Your fragment F could be recreated or resumed too depending on how you manage the fragment in your Activity A, so you would lose all the information stored in editTexts.

It would be usefull if you post the code of your Activity A.

The best way to keep the data of fragments between transitions, is store it in your Activity and get it from the Fragment.

In your Fragment

  @Override
  public void onResume(){
       EditText1.setText(getActivity().getTextOfEditText1());
       EditText2.setText(getActivity().getTextOfEditText2());
       super.onResume();
  }

** And in your Activity**

 public class A extends Activity{
       //...
       private String textOfEditText1;
       private String textOfEditText2;


       //... Other stuff

       public String getTextOfEditText1(){
              return textOfEditText1;
       }
       public String getTextOfEditText2(){
              return textOfEditText2;
       }

 }

You should have methods to set the Strings from the Fragment when you modify them.

Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM