3

I have MainActivityFragment from where I start DetailActivity with below code:

private final CurrencyListAdapter.ClickListener itemClickListener
        = new CurrencyListAdapter.ClickListener() {
    @Override
    public void onItemClick(int position, View v) {
        Intent intent = new Intent(getActivity(), DetailActivity.class);
        String code = mCurrencyList.get(position).getCode();
        intent.putExtra(Constants.SELECTED_CODE_KEY, code);
        startActivity(intent);
    }
};

After that from my DetailActivity I start my DetailActivityFragment with below code:

if (savedInstanceState == null) {
        DetailActivityFragment fragment = DetailActivityFragment.getInstance(code);
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.detail_container, fragment)
                .commit();
    }

Also I am retaining the instance state within my MainActivityFragment. However, while hitting back button from DetailActivityFragment, onCreateView method of my MainActivityFragment is called and I am not able to restore instance state, because saveInstanceState method is not executed and thus the savedInstanceState is null. I am very curious why this happening. This is not happening in other project wich has the same logic. I will be very thankful if anyone could help me.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Orkhan Gasimli
  • 103
  • 1
  • 11

1 Answers1

0

I have already figured it out. I was loading MainActivity through xml layout file. I changed this and included the following code into my MainActivity and after the issue is gone:

if (savedInstanceState == null) {
    MainActivityFragment fragment = new MainActivityFragment();
        getSupportFragmentManager()
        .beginTransaction()
        .add(R.id.main_container, fragment)
        .commit();
}
Orkhan Gasimli
  • 103
  • 1
  • 11