Refresh Activity prior to ending Current Activity

1

Good morning, I'm developing an Android project and I'm having a problem I'll try to explain clearly:

I have two Activity A and B, my Activity A calls B and stays in the stack, after closing the actions on 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 in A, so I need to update it to display those changes when I finish B. Is there any way to do this update or reload Activity A as soon as B is completed?

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

    
asked by anonymous 26.04.2016 / 15:49

2 answers

4

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

You can put all the communication with the web service in the method onResume() , so that when the Activity is created the data loading happens and when the activity is 'restarted' also happens. You will always have the data updated. It is in this sense that I always recommend dropping data in onResume() so that the screen is always up to date.

    
26.04.2016 / 16:06
2

Hello, you can call Activity B, using the StartActivityForResult

Activity A

private static final IDENTIFICADOR_B = 1;

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

Override activity onA onActivityResult

@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, end like this:

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

I hope I have helped ...

    
26.04.2016 / 16:00