1

I have application with several activities with common Views (a few Buttons and a TextView). I've created the common View and handled the actions in abstract class that extends Activity and all my activities extends my abstract class.

My problem was when I switched between activities and changed a text on a Button. For example, when I pressed "back" button the previous Activity didn't have the "updated" text on the Button. I added in my abstract class onActivityResult() method and changed in all activities to open new activity from startActivity to startActivityForResult. Now it's working fine but I found out that there's also onResume() method that can handle my first problem.

So my question is: what is better? or even more accurate: what is the right way to handle my problem? thanks

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • related http://stackoverflow.com/questions/18243515/android-going-back-to-previous-activity-with-different-intent-value/18243541#18243541 – codeMagic Jan 13 '15 at 18:11
  • but in my case I don't have a "specific related data" to pass from the activity. my button is the log in button, which opens log in activity if no user logged in or do log out if the user is logged in, if the answer is still the same, why would we ever need then the onResume method? – Seriy Alexandrovski Jan 13 '15 at 20:17
  • There are a lot of reasons for `onResume()`. But it is called directly after `onCreate()` **and** when returning to the Activity unless `onActivityResult()` is called. I don't understand your exact scenario so I can't really give you the answer. Data doesn't need to be sent back to use it and I didn't know your specific case which is why I didn't close as a dupe but linked to it instead. – codeMagic Jan 13 '15 at 20:20
  • in order to know what to write on the button (Login or Logout) I use a method check() which I call every time I enter activity (the parse user is defined on abstract class so in all activities) and I don't send "special" data from activity X to activity Y, so I'm still confused on what method is more "programmatic right" way to use? – Seriy Alexandrovski Jan 13 '15 at 20:27
  • Then calling `check()` in `onResume()` would be what you want – codeMagic Jan 13 '15 at 20:30
  • so it is using onResume() if no specific data needed to transfer from activity X to activity Y and if needed then onActivityResult(), thank you very much! – Seriy Alexandrovski Jan 13 '15 at 20:37
  • Basically, yes. I can try to write up an answer in a bit to clarify things a little better if you'd like – codeMagic Jan 13 '15 at 20:39

1 Answers1

1

Since you aren't needing to return any data and the text that the Button should have is obtained in a function called every time the Activity opens, simply using onResume() will suffice.

onResume() is called each time the the Activity runs, after onCreate() runs, but it is also called each time the Activity comes to the foreground (say, by hitting the back button from the activity in front of it).

what is the right way to handle my problem?

So, while you could use either, there is no need here for startActivityForResult(). It would just add unnecessary code and overhead (even though I doubt your users would see a difference). Just add the method which checks for the text that the button needs to onResume().

onResume() Docs

When you do want to return data from a called activity and take specific action depending on that data, I've got an example in this answer

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93