Amit Tiwari
Amit Tiwari

Reputation: 3692

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);
    }

Upvotes: 4

Views: 13215

Answers (4)

Shashank Singla
Shashank Singla

Reputation: 1837

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.

Upvotes: 6

Ashish Shukla
Ashish Shukla

Reputation: 1047

I think you should get the number of comments from the backend for that feed and save to a variable and pass it through intent and then you can place it on the total comments for that feed.

Bcoz intents are the fastest way to send data across activitys

Upvotes: 1

throws_exceptions_at_you
throws_exceptions_at_you

Reputation: 1746

As can be seen in the activity lifecycle the ideal place for doing this is in the onRestart() callback method:

  @Override
    protected void onRestart() {
        super.onRestart();
        // this is your backendcall
        backendCall();
    }

Activity Lifecycle

Upvotes: 2

fweigl
fweigl

Reputation: 22018

You can start your Activity B 'for Result' with

int activityBrequestCode = 0; 
startActivityForResult(intent, activityBrequestCode);

and in your Activity B set a result when the back button is pressed with

@Override
public void onBackPressed() {

   setResult(RESULT_OK);

   super.onBackPressed();
}

Then your Activity A will be informed in

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == activityBRequestCode && resultCode == RESULT_OK){
        // refresh
    }
}

Upvotes: 3

Related Questions