Update Activity previous to end Current Activity

Asked

Viewed 3,031 times

1

Good morning, I am developing an Android project and am with a problem I will try to explain clearly:

I have two Activitys A and B, my Activity A calls B and stays in the stack, after terminating the actions in B this ends and returns to A (returns because A was in the stack). This Activity B changes data on the server that changes the display on A, so I need A to update when B is finished to display such changes. Is there any way to do this update or reload Activity A once B is finished?

Detail: Before Activity A there are others on the stack.

2 answers

4


Paul, I think the simplest and most elegant way in your case is to use Activity’s own life cycle for this. When Activity B is finished, Activity A, which was in stop, will have its life cycle covered in order to make it suffer 'Restart'. The main methods called will be onRestart() -> onStart() -> onResume(). Note that the onResume() method is called in the creation of Activity and in its 'Restart'!

You can put all communication with the web service in the method onResume(), so that when the Activity is created the data load will happen and when Activity is 'restarted' will also happen. You will always have the data updated. It is in this sense that I always recommend to leave the data upload on onResume() so that the screen is always updated.

  • 1

    Taxis, I used the onRestart method in Activity A and it worked perfectly, when returning it calls the method that interacts with the web service and thus loads the updated data. Thanks for the help!

  • Great, Paul! onRestart also solves your problem! It was a pleasure to help you!

2

Hello, you can call Activity B, using Startactivityforresult

Activity A

private static final IDENTIFICADOR_B = 1;

Intent i = new Intent(this, ActivityB.class);
startActivityForResult(IDENTIFICADOR_B  1);

Over Activity A the onActivityResult method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == IDENTIFICADOR_B ) {
        if(resultCode == Activity.RESULT_OK){
            String resposta = data.getStringExtra("tag"); //resposta = "atualizar"
           //Atualizar mais coisas aqui
        }
    }
}

In your Activity B, finish like this:

Intent returnIntent = new Intent();
returnIntent.putExtra("tag","atualizar");
setResult(Activity.RESULT_OK,returnIntent);
finish();

I hope I’ve helped...

  • 1

    An alternative is to use the method onStart() or onResume() who will be called upon to return to Activity To, after B finalize.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.