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
Override the
onRestart()
method so that it is called automatically when you return back from your desired task.
Put the following code in your onRestart() method :
@Override
protected void onRestart() {
super.onRestart();
finish();
overridePendingTransition(0, 0);
startActivity(getIntent());
overridePendingTransition(0, 0);
}
Call child activity using startActivityForResult with request code,SetResult from the child Activity. And when the child activity is finished, you can update you parent activity in onActivityResult method
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
//Check the result and request code here and update ur activity class
}
Here is an example http://rahulonblog.blogspot.com/2010/05/android-startactivityforresult-example.html
Another tricky way to do this is just start your activity on onRestart()
@Override
public void onRestart(){
super.onRestart();
Intent previewMessage = new Intent(StampiiStore.this, StampiiStore.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("StampiiStore", previewMessage);
this.finish();
}
That should do the trick actually. (In this code I'm showing the way it's done when you are using a custom TabActivity manager.)
If you start your second activity with the method startActivityForResult
, when you return to the first activity, onActivityResult
of the first activity will be called.
If you override it, you can refresh your activity from there.
See more information here and here
Override onRestart()
method inside activity you want to refresh, and recreate the activity
@Override
protected void onRestart() {
super.onRestart();
this.recreate();
}
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();