in Education by
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
by
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.

Related questions

0 votes
    I have a fragment with a view and an options menu: public class OfferingsFragment extends Fragment { public ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I'm designing a service that will comprise a cloud-based database (with a UI for us to manage the data), ... sit behind mobile apps. Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    I want to put a settings icon to the right of a full-width button which when clicked opens an ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    The text on my buttons are set in resource files. E.g. The above code defines a button with text ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    State T' for true and F' for false statements: 1. Home button is used to display the home page. 2. ... be used to pay bills. Select the correct answer from above options...
asked Dec 10, 2021 in Education by JackTerrance
0 votes
    I am struggling to get my AppCenter to build my Android app, so I wanted to try and understand a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    I'm trying to use the Android GraphView library (http://www.android-graphview.org/), but no matter ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I am seeking an Android solution to calculate the distance to an object and then determine the size (height ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    How to remove a image in imageview in android and also how to hide the entire image. Here I have ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 27, 2022 in Education by JackTerrance
0 votes
    I am trying to build a mailto: uri to send a mail using the GMail app. I would like to use the ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I'm looking at making an custom Android object Serializable. Should be simple I just cannot find a easy ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I've made a fresh eclipse reinstall + Android SDK v4.0. I am trying to create a new 4.0 AVD ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I'm trying to execute the following command: arp -a and I would like to redirect the output to a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    Here some snippet of my code. when first time RecyclerView create I have to used recyclerView.scrollToPosition( ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I've noticed on Marshmallow (e.g. Nexus 6P) and also on some more recently updated Lollipop phones ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
...