Back button and refreshing previous activity

后端 未结 11 1339
感动是毒
感动是毒 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:45
     @Override
    protected void onRestart() {
        super.onRestart();
        finish();
        overridePendingTransition(0, 0);
        startActivity(getIntent());
        overridePendingTransition(0, 0);
    }
    

    In previous activity use this code. This will do a smooth transition and reload the activity when you come back by pressing back button.

    0 讨论(0)
  • 2020-12-02 06:46

    If not handling a callback from the editing activity (with onActivityResult), then I'd rather put the logic you mentioned in onStart (or possibly in onRestart), since having it in onResume just seems like overkill, given that changes are only occurring after onStop.

    At any rate, be familiar with the Activity lifecycle. Plus, take note of the onRestoreInstanceState and onSaveInstanceState methods, which do not appear in the pretty lifecycle diagram.

    (Also, it's worth reviewing how the Notepad Tutorial handles what you're doing, though it does use a database.)

    0 讨论(0)
  • 2020-12-02 06:46

    Try This

     public void refreshActivity() {
        Intent i = new Intent(this, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        finish();
    
    }
    

    or in Fragment

      public void refreshActivity() {
        Intent i = new Intent(getActivity(), MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        finish();
    
    }
    

    And Add this method to your onBackPressed() like

      @Override
    public void onBackPressed() {      
            refreshActivity();
            super.onBackPressed();
        }
    }
    

    Thats It...

    0 讨论(0)
  • 2020-12-02 06:49

    I would recommend overriding the onResume() method in activity number 1, and in there include code to refresh your array adapter, this is done by using [yourListViewAdapater].notifyDataSetChanged();

    Read this if you are having trouble refreshing the list: Android List view refresh

    0 讨论(0)
  • 2020-12-02 06:51
    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this,DesiredActivity.class);
        startActivity(intent);
        super.onBackPressed();
    }
    
    0 讨论(0)
提交回复
热议问题