There is Activiti, fragments are created in it. In fragments of the view. Suppose we entered something in the input field. Press the Home button, the application collapses, expands back, the entered text is no longer there.

How to avoid this feature?

  • one
    Most likely, the application is simply unloaded from memory, since this in fact should not happen. - Sergey

2 answers 2

View with id must save and restore their state automatically ( within the application session ), but if this does not happen, you can implement the state saving manually, for example:

 public class MainActivity extends AppCompatActivity { private static final String EDIT_TEXT_SAVE_KEY = "EDIT_TEXT_SAVE_KEY"; private EditText mEditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEditText = (EditText) findViewById(R.id.edit_text); if (savedInstanceState != null) { String savedText = savedInstanceState.getString(EDIT_TEXT_SAVE_KEY); mEditText.setText(savedText); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString(EDIT_TEXT_SAVE_KEY, mEditText.getText().toString()); } } 

In order to avoid boilerplate code, you can use Icepick .

If this advice does not help, then the application is unloaded from memory, and resetting the state of the application is normal. If, on the SharedPreferences , it is important to save data from input fields, you can use, for example, SharedPreferences .

    In my applications for this kind of tasks, I always use these parameters in my main activity:

     <activity android:name=".MainActivity" ... android:taskAffinity="" android:launchMode="singleInstance" android:configChanges="keyboardHidden|orientation|screenSize" ...> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> 

    You can read more here: https://developer.android.com/guide/topics/manifest/activity-element.html

    • one
      This is a bad decision, as there are so many situations, besides keyboardHidden|orientation|screenSize , in which the activit can be recreated. For example, the system can nail the activation when the application is in the background, and when you return to this activation, its initial state is displayed. - post_zeew
    • But oddly enough, it works even in an application that consumes 200+ MB of RAM. Ie being in the background for a long time. Again, the author does not mean data saving, he means View’s preservation, that is, so that the activity does not recreate at all, going to the background and back. And this method really works. - Sergey
    • one
      Consider yourself lucky - for example, a lot of free RAM. In general, this will not work. By setting do not keep activities in the developer options you can check my words. - post_zeew
    • Well, here Navadsky himself will not help)) and then the author is only onSaveInstanceState to help - Sergey