When running an Activity I want the execution to wait for the shutdown to continue, or run sequentially the code

Asked

Viewed 271 times

1

How do I run an Activity and after closing it continue executing the code from where it stopped, example:

    listmarcacoes.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView adapter, View viw, int posicao,long id) {
            cursor.moveToPosition(posicao);
            Intent intent = new Intent(getActivity(), EdicaoMarcacao.class);
            Bundle params = new Bundle();
            String resposta = cursor.getString(1).toString();
            if(CalculoHora.isHojeData(resposta)){
                Toast.makeText(getActivity(), "Use a aba RESGISTRO para marcações do dia!", Toast.LENGTH_SHORT).show();
            } else {
                params.putString("diafiltro", resposta);
                intent.putExtras(params);
                //  Aqui quero que a execução aguarde a activity ser executada!
                startActivity(intent);
                //  Aqui a execução continua apos o encerramento da actitity!
                // Executanto posteriormente o metodo lista()
                lista();
            }
        }            
    });

I need it this way because when I call Activity "Edicaomarcao" it changes the values of listview , this way when I close the second screen I want you to update the first.

1 answer

1


You can use:

listmarcacoes.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView adapter, View viw, int posicao,long id) {
        cursor.moveToPosition(posicao);
        Intent intent = new Intent(getActivity(), EdicaoMarcacao.class);
        Bundle params = new Bundle();
        String resposta = cursor.getString(1).toString();
        if(CalculoHora.isHojeData(resposta)){
            Toast.makeText(getActivity(), "Use a aba RESGISTRO para marcações do dia!", Toast.LENGTH_SHORT).show();
        } else {
            params.putString("diafiltro", resposta);
            intent.putExtras(params);
            startActivityForResult(intent, 9);//9 é o requestCode número inteiro para a identificação
        }
    }            
});

Then overwrite the method:

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

    if (requestCode == 9 && resultCode == RESULT_OK) {

        Bundle params = data.getExtras();
        String resposta = params.getString("diafiltro");
        lista();
    }
}

At Activity Edicaimarcao do:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle params = getIntent().getExtras();
    String resposta = params.getString("diafiltro");
}

Then make the changes you want and when you finish running Activity Edicaimarcao.class do:(Obs:na Edicaimarcao.class)

Bundle params = new Bundle();
params.putString("diafiltro", resposta);
intent.putExtras(params);
setResult(RESULT_OK, intent);
finish();
  • Just to confirm, the code "onActivityResult" I put in the current Activity, right? , already the code for when finished put in Edicaimarcao.class, correct? but which Edicaimarcao.class method can I put in?

  • That is correct. You take the Bundle passed by the current Activity and make changes in Edicaimarcao.class and then pass your answer through the Bundle, and then you finish this Activity.. then in the onActivityResult method you take the answer that is in the Bundle and then call the list() method to update the values of your listview.

  • It worked. Thank you!

Browser other questions tagged

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