1

Would like to check with you something about activity and fragment lifecycle.

Assuming I have Activity A calling Activity B. Inside Activity B, I have Fragment C and Fragment D separated by Tabs.

Fragment C will load spinner values downloaded from web API. After user press back button to Activity A and then to Activity B again, i do not want to reload the spinner and call web API to populate its values.

Where should I put the code to initialize spinner only once at Fragment C in this scenario?

Here is how I create Activity B

public class Activity_B extends custom_activity{

final Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

    viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
    tabLayout.setupWithViewPager(viewPager);
}

  public class SectionPagerAdapter extends FragmentPagerAdapter {

    public SectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new Fragment_C();
            case 1:
            default:
                return new Fragment_D();
        }
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getResources().getString(R.string.fragment_c);
            case 1:
            default:
                return getResources().getString(R.string.fragment_d);
        }
    }
}

}
Bih Cheng
  • 655
  • 1
  • 13
  • 28

1 Answers1

0

You can achieve this behavior using android:launchMode="singleInstance"

        <activity
        android:name="com.aitrich.android.framework.ui.activity.HomeActivity"
        android:label="@string/app_name"
        android:launchMode="singleInstance"
        android:screenOrientation="portrait"   >
    </activity>

using android:launchMode="singleInstance" flag in ActivityB will create only single instance of that activity..so you can retain the data when navigating from ActivityA again..

NOTE : Assuming that you are not finishing ActivityB

darwin
  • 1,524
  • 1
  • 22
  • 32
  • my spinners gets populated inside a fragment C on Activity B. I tried putting singleInstance to Activity B and its still calling onCreateView each time i goes from Activity A to Activity B – Bih Cheng Oct 26 '15 at 09:41
  • make sure that you are not finishing ActivityB when navigating to ActivityA. – darwin Oct 26 '15 at 09:47
  • Yes, i'm using getActivity().finish() to return startActivityForResult from Activity A. How should i structure my code in this scenario for this to work? – Bih Cheng Oct 26 '15 at 09:55
  • you can replace the activtyForResult approch by passing data via intent. return the result back to activtyA using intent.putExtra method. – darwin Oct 26 '15 at 10:25
  • check this link http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android – darwin Oct 26 '15 at 10:33