How to detect if came back from child activity?

后端 未结 4 1058
南旧
南旧 2021-02-20 06:35

How can I detect if an activity came to focus after pressing the back button from a child activity, and how can I execute some code at that time?

4条回答
  •  一生所求
    2021-02-20 07:12

    You can also override both the onBackPressed() method and the onOptionsItemSelected() method and put some logic there. For example I put this into my BaseActivity which all the other Activities extends from:

    @Override
    public void onBackPressed() {
        // your logic
        super.onBackPressed();
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            // your logic
        }
        return super.onOptionsItemSelected(item);
    }
    

提交回复
热议问题