There is a MainActivity , which starts to work when you start the application. Then I run EditActivity , and in it I enter certain values ​​into the database. When I press the back button, I return to the MainActivity .

How can I execute a certain getFromDb() function in an already running Activity ? It may be necessary to restart it by running onCreate() , but again, how?

    2 answers 2

    When you start EditActivity, use startActivityForResult, and after closing the second activation, execute getFromDb () in the onActivityResult () method.

    • intRequestCode can be any? Is it an id? - Mr Klonwar
    • one
      @MrKlonwar Moreover, you can branch on the transmitted RequestCode that if no changes were made to EditActivity (send 1 if there were changes and 2 - if not, for example), then you should not update the database in onActivityResult() . This mechanism is specifically designed for this purpose, in order not only to receive a callback on return, but also to take different actions depending on the results of the work of the activated activation. - pavlofff
    • one
      @NikotinN see the article at the link in my comments above. ResultCode - operation success; RequestCode - marking of the returned result. - pavlofff
    • one
      @pavlofff, RequestCode is assigned when you start the second activation, and you say that with it you can track whether there were changes during the second activation. RequestCode is needed that in onactivityresult to understand from which query the result came. In your article using requestcode, the result is distinguished from two different activations, and not what you are talking about. - Nikotin N
    • one
      And for sure, I did not wake up yet. then ResultCode - pavlofff

    If I understand correctly, then you want to handle the event return to Activity onStart() , or, perhaps, onRestart() :

     public class MainActivity extends Activity { // Переопределение обработчика события onStart() @Override protected void onStart(Bundle savedInstanceState) { super.onStart(savedInstanceState); getFromDb(); // Вызов желаемого метода обработки события } // Переопределение обработчика события onRestart() @Override protected void onRestart(Bundle savedInstanceState) { super.onRestart(savedInstanceState); getFromDb(); // Вызов желаемого метода обработки события } } 

    State transition graph Activity :

    enter image description here

    • This decision is not entirely correct, since reading the table will occur not only when returning to activations, but also during turns, for example, which is not required at all and, at best, will waste resources on currently unnecessary actions. - pavlofff
    • @pavlofff: as far as I know, onRestart() only works when you return to the Activity from the onStop() state, and it does not work when you rotate the screen: when you rotate, the Activity passes through the tops of onDestroy() and onCreate() . - Artyom Ionash
    • @pavlofff: I just checked: when turning the screen, onRestart() not called, you can see for yourself. I suspect you are a little confused. - Artem Ionash
    • I now have no opportunity to check this, unfortunately, but in any case, the first answer, with a call to read the database on the return event from the activation, which made the changes, is a more correct and logical solution than the hardcore update in the life-cycle method that is called during a set of actions . - pavlofff
    • @pavlofff: the first solution is less logical: an empty completion code of the second Activity is entered with it, which allows only to catch the completion event of a particular Activity , in case there are any other Activity that do not have this opportunity. - Artyom Ionash