Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh activity on back button

I have an activity A which has a feed kind of interface. There is a text on each of these feed items which indicate the number of comments on that item. Now if on that item, I click comment and go to new activity B, add comment through activity B and press back to go back to activity A. When I add the comment, the info is reflected in the backend. But the problem is, when I press back, the saved instance of activity A shows in which the number of comments is not updated because there is no call to backend from activity A.

What is the standard way of doing something like this? How should I refresh the previous screen in activity stack on pressing back on some activity?

Here are some relevant code snippets:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.gallery_page_button_back);


@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if(id == android.R.id.home) {
            this.finish();
            return true;
        }

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
like image 204
Amit Tiwari Avatar asked Oct 26 '25 11:10

Amit Tiwari


1 Answers

onPause() is called in Activity A when it launches Activity B. After the back button is called in Activity B, onResume() is called in Activity A.

You should load comments (api/server call) in onResume function rather than onCreate function of Activity A so that every time the activity is resumed your comments are refreshed.

like image 148
Shashank Singla Avatar answered Oct 28 '25 01:10

Shashank Singla