Android Refresh activity when returns to it

Android-Droid picture Android-Droid · Nov 23, 2011 · Viewed 53.4k times · Source

I need a little help with refreshing one of my activities in my application. I'm using tab host activity and connecting to web service and downloading some data from one of my child activities. When I press sync button in my child activity I'm starting a new activity which is not in tab host and when the sync is done, it returns to it's parent (child activity). The thing that I want to achieve is to refresh the activity when I return back to it. As I checked over the internet I find that the best option to do it is using startActivityForResult,but I don't really understand how to use that and how to refresh the activity when I receive the result from the finished activity.

If anyone can help me, I'll be really glad.Thanks!

EDIT:

I'm using this code and it's not even showing the Log in onActivityResult

MyCollectionId.class :

Intent intent = new Intent(MyCollectionId.this, Synchronization.class);
intent.putExtra("process", 2);
startActivityForResult(intent, 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 if(resultCode==RESULT_OK){
     Log.e("","OnActivityResult");
    Intent refresh = new Intent(this, MyCollectionId.class);
    startActivity(refresh);
    this.finish();
 }
}

Synchronization.class :

Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
setResult(RESULT_OK,intent);
finish();

Answer

user849998 picture user849998 · Nov 23, 2011

on button press:

Intent intent = new Intent(this, SyncActivity.class);
        //intent.putExtra("someData", "Here is some data");
        startActivityForResult(intent, 1);

Then in the same Activity class:

   @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if(resultCode==RESULT_OK){
         Intent refresh = new Intent(this, InitialActivity.class);
         startActivity(refresh);
         this.finish();
      }
     }

The Sync Activity would have:

setResult(RESULT_OK, null);
finish();