0 votes
ago in Education by (1.7m points)
I have a fragment with a view and an options menu:

public class OfferingsFragment extends Fragment

{

     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)

     {

         setHasOptionsMenu(true);

         View view = inflater.inflate(R.layout.offering_tiles, null);

         ...

     }

     @Override

     public boolean onOptionsItemSelected(MenuItem item) {

          int id = item.getItemId();

          if (id == R.id.action_settings) {

               Intent intent = new Intent(getActivity(), SettingsActivity.class);

               startActivity(intent);

               return true;

          }

          return super.onOptionsItemSelected(item);

      }

}

From the options menu, the user opens this preference fragment, which is hosted by the SettingsActivity:

public class SettingsActivity extends Activity {

    private SettingsFragment settingsFragment = null;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         settingsFragment = new SettingsFragment();

         getFragmentManager().beginTransaction()

            .replace(android.R.id.content, settingsFragment)

            .commit();

}

The view of the OfferingsFragment depends on one of the preferences. That is, after this preference has changed, the OfferingsFragment must be refreshed by calling onCreateView again. What I do is this:

Open preference screen from OfferingsFragment's option menu

Change preference

Return to OfferingsFragment

If I return to the OfferingsFragment via the Home Button (left arrow in ActionBar), then the OfferingsFragment gets refreshed by calling its onCreateView (which is the desired effect). However, if I return to the OfferingsFragment via the Back Button (on the device), onCreateView is NOT CALLED and thus the view is NOT re-created. What I want is that the view is also re-created when the user presses the Back Button. Any ideas how to achieve this?

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
ago by (1.7m points)
What Happens

When you press Up button parent activity is called via startActivity which means a new instance is created by default.

When you press Back button current activity is finished and you're back in the previous activity and its already existing instance (it was in stopped state).

How To Deal With It

What I want is that the view is also re-created when the user presses the Back Button. Any ideas how to achieve this?

Start the settings activity via startActivityForResult:

public static final int RC_SETTINGS = 1;

private void startSettingsActivity() {

    Intent i = new Intent(this, SettingsActivity.class);

    startActivityForResult(i, RC_SETTINGS);

}

When the result comes back reattach the fragment. This will recreate its view hierarchy.

@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    // Call to super if you value your life. And want proper lifecycle handling.

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SETTINGS) {

        // If we just came back from SettingsActivity...

        // ...reattach fragment and trigger view recreation.

        final FragmentManager fm = getFragmentManager();

        final f = fm.findFragmentById(android.R.id.content);

        fm.beginTransaction().detach(f).attach(f).commit();

    }

}

Replace the fragment ID with whatever you used.

Pro tip

If your fragment is not misconfigured you should be able to call

public class SettingsActivity extends Activity {

    private SettingsFragment settingsFragment = null;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {

            settingsFragment = new SettingsFragment();

            getFragmentManager().beginTransaction()

                .replace(android.R.id.content, settingsFragment)

                .commit();

        } else {

            settingsFragment = (SettingsFragment) getFragmentManager().findFragmentById(android.R.id.content);

        }

    }

}

This is both resourceful and practical as your original code would lose state (for example scroll position) on configuration change.
...