Search code examples
androidonresumeactivity-lifecycle

Android: activity performing unwanted function during onResume()


I'm making an app with a splashscreen animation. I have given the user the option of canceling the animation.

The home screen (Home) has a TextView (tv) . I set the text during the onCreate(), and again during the onResume() .

When running with the animation, the flow is: splashscreen -> home screen -> other activities. The text of tv is set, and then returning to the home screen it 'reloads' and sets different text.

When the animation is canceled, it starts with the home screen: home screen -> other activities.

THE PROBLEM: When animation is canceled, the home screen sets the text in tv2 during the onCreate() and then SETS IT AGAIN in the onResume(), even though I have added an 'if' clause to prevent this from happening.

Here is come code:

public class Home extends Activity {
    public static boolean reload = true;
    EditText tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    tv= (EditText) findViewById(R.id.textViewFact);
    tv.setText(updateText()); 
    MORE STUFF ....
}

The 'reload' is set to false from a different activity, if the user chooses to cancel the animation.

and now for the onResume:

public void onResume(){
    super.onResume();
    Log.i("test", "home activity before if: " + Home.reload);

    if(!reload){
        Log.i("test", "home activity inside if: " + Home.reload);

        return;
    }
    else{
        Log.i("test", "home activity inside else: " + Home.reload);

        tv.setText(updateText()); 
    }
}

As you can see, the updateText() runs in the onCreate() and the onResume(), yet if reload is false (which it IS, as you can see in the following logcat output), it SHOULD NOT be running the updateText() again.

01-30 08:44:50.140: I/test(551): home activity before if: false
01-30 08:44:50.140: I/test(551): home activity inside if: false
01-30 08:44:51.170: I/test(551): home activity before if: false
01-30 08:44:51.170: I/test(551): home activity inside if: false

I don't know why it is getting into the onResume() TWICE (which it is, by the log output), and how it is even running the updateText() function, when that is only inside the ELSE clause, which isn't reached, according to the log output.


Solution

  • It turns out that the updateText() was being called again, not from the onResume() but from the onCreate(), as the activity that called the home screen was not finished properly, and actually calling the home activity to start twice.

    Thank you all for your help.