Back button and refreshing previous activity

后端 未结 11 1378
感动是毒
感动是毒 2020-12-02 06:01

If we have two activities:

  1. List of files and last modified time
  2. File editing activity

A user selects a file from the list and is taken

11条回答
  •  隐瞒了意图╮
    2020-12-02 06:40

    One option would be to use the onResume of your first activity.

    @Override
    public void onResume()
        {  // After a pause OR at startup
        super.onResume();
        //Refresh your stuff here
         }
    

    Or you can start Activity for Result:

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

    In secondActivity if you want to send back data:

     Intent returnIntent = new Intent();
     returnIntent.putExtra("result",result);
     setResult(RESULT_OK,returnIntent);     
     finish();
    

    if you don't want to return data:

    Intent returnIntent = new Intent();
    setResult(RESULT_CANCELED, returnIntent);        
    finish();
    

    Now in your FirstActivity class write following code for onActivityResult() method

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
      if (requestCode == 1) {
    
         if(resultCode == RESULT_OK){      
             //Update List         
         }
         if (resultCode == RESULT_CANCELED) {    
             //Do nothing?
         }
      }
    }//onActivityResult
    

提交回复
热议问题