Search code examples
androidviewonresume

how to find contentView in onResume


In my app I have two views, a main and a secondary. However, if my app is paused while in the second view it crashes when the on resume code runs. I discovered that this was because in my on resume has a line where it sets a value to a textview. I would like to encase it in an if statement, but I do not know what phrase I should use to find the content View. Sorry if I am a little unclear - when my user is in secondary view and they close then reopen the app, running the on resume code, it crashes because the text view that the onResume method is trying to modify cannot be accessed.(as it is my main view) and so it crashes.

I know that I could use a try-catch statement, but I also need to find the view in a place in my app where I can't use a try-catch.


Solution

  • I solved my problem by keep in an int value at zero whilst in one view, and whenever the view is switched, I changed the value of that int to one. Then in my onResume, if that ints == 1, then I don't set the value of my edittext, but if that int == 0, then I know that it is safe to call the setText() method