1

I am using onStop() to save a boolean value which I need when the activity resumes.

Here is my code:

@Override
protected void onStop()
{
    super.onStop();

    Bundle bundle = new Bundle();
    bundle.putBoolean("value",value);
    getIntent().putExtras(bundle);

}

@Override
protected void onResume()
{
    super.onResume();

    if(getIntent().getExtras() != null)
    {
        Bundle bundle = getIntent().getExtras();
        value = bundle.getBoolean("value");
    }



}

My issue is no matter what the value of the boolean is, my onResume() always retrieves it as FALSE. This issue only occurs if I leave my activity using the BACK button. If I press home, things seem to work fine(i.e if the boolean was TRUE then onResume() retrieves it as TRUE.

Please do help me because I don't understand why onResume() always gets the value of the boolean as FALSE even when I save it as TRUE in onStop().

I also tried onRestart(), onPause() and onBackPressed() but I still can't get the proper boolean value to be saved.

4 Answers4

1

You have two issues here.

  1. the correct way to save values during activity destruction is to use onSaveInstanceState(Bundle) and get the value from the Bundle passed to onCreate(Bundle).

Check example below:

public class SavedInstanceExample extends AppCompatActivity {

    private boolean myBoolean;

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

        if (savedInstanceState != null) {
            myBoolean = savedInstanceState.getBoolean("key");
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("key", myBoolean);
    }
}
  1. When you press the back button the activity will be finished. That means completely gone. And values saved one the methods explained above will not be there.

The way to save something to survive the Activity being finished is to save it to the disk. One common/simple way to do it is using the SharedPreferences

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Can you give an example on the 1st point. Because I tried using onSaveInstanceState and onRestoreInstanceState and it did not work either. – Mostafa Abdulrazic Nov 19 '17 at 17:52
  • Here is a example handling screen rotation by saving the state in Bundle - https://stackoverflow.com/q/47345425/8199772 – HB. Nov 19 '17 at 17:56
  • Excellent answer @Budius – HB. Nov 19 '17 at 17:56
  • @MostafaOsama I've edited the answer with an example. – Budius Nov 19 '17 at 18:01
  • @MostafaOsama the second point. This method is only to save values for activity destruction due to rotation or system memory management. Using the back button you're finishing the activity and THE ONLY way is to save the value to disk. – Budius Nov 19 '17 at 18:10
  • https://stackoverflow.com/questions/23919338/how-to-store-a-boolean-value-using-sharedpreferences-in-android – Budius Nov 19 '17 at 18:13
0

When the Android application opens the following activity lifecycle methods will be called.

onCreate(); onStart(); onResume();

and when you press the back button, the application will be destroyed by calling following methods

onPause(); onStop(); onDestroy();

And in the second case when you press home button the following methods will be called onPause(); onStop(); That means your application is not destroyed completely and you can open it from recent apps so that the activity re-appears by calling onStart(); onStop(); That is why your code works in this case.

Activity gives onSavedInstanceState() method to save your data during configuration changes or something else.

Here is the link for Android documentation for Activity

Sanket Bhat
  • 340
  • 3
  • 16
0

I would suggest you to read the Google Developers Link for Activity documentation.
Google Developers Activity

ManmeetP
  • 801
  • 7
  • 17
0

The OP's code is basically right. onSavedInstanceState is no good if you are not destroying the activity but, for example, replacing a fragment in an activity with another fragment and then returning to it, in which case you have to use onStop and onResume as follows. This is Kotlin and it works but the principle is the same.

override fun onStop() {
    super.onStop()
    val bundle = Bundle()
    bundle.putBoolean("BOOL", false)
    activity?.intent?.putExtras(bundle)
}

override fun onResume() {
    super.onResume()
    if (activity?.intent?.extras != null) {
        val bundle = activity?.intent?.extras
        val bool = bundle?.getBoolean("BOOL")
        println("BOOL is $bool")
    }
}

My Java's a bit rusty but I suspect the OP's problem might have been that he mixed up boolean and Boolean?

eggdeng
  • 148
  • 9