Search code examples
androidonactivityresult

how can refresh Activity when I finish Activity then close Activity?


how can refresh Activity when I finish Activity then close Activity?

I have two Activity,

The first one is GroupInfoActivity, when users submit information in GroupPostActivity, it's will finish GroupPostActivity then back to GroupInfoActivity.

in the GroupInfoActivity I have set onActivityResult

final int RESULT_CODE=101;

 @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode ==RESULT_CODE) {
            setResult(1,(new Intent()));
        }
    }

And the GroupPostActivity I have set

final int RESULT_CODE=101;
Intent intent=new Intent(GroupPostActivity.this,GroupInfoActivity.class);
intent.putExtra("gid",getIntent().getStringExtra("gid"));
intent.putExtra("groupname",getIntent().getStringExtra("gname"));
setResult(RESULT_CODE, intent);
finish();

But when I finish GroupPostActivity, the GroupInfoActivity cannot refresh and get new data.

where has problem?

I dont want to use startActivity(intent) this method, because it's more avtivity.


Solution

  • GroupInfoActivity

        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if(resultCode ==RESULT_CODE) {
    
                if (data != null) {
    
                    String gid = data.getStringExtra("gid");
                    String groupname = data.getStringExtra("groupname");
    
                    //refresh or do anything you want
                }
            }
        }