Android Refresh activity when returns to it
Asked Answered
C

7

16

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();
Claustrophobia answered 23/11, 2011 at 10:53 Comment(7)
what do you mean by: 'it's not even showing the Log in'?Nephridium
I mean this Log : Log.e("","OnActivityResult");Claustrophobia
try to move it before if conditionNephridium
can you try changing to like: setResult(1); finish(); in Synchronization, so don't send intentNephridium
the same result, not showing onActivityResult()Claustrophobia
I just tried this code with some app that has tabhost and it actually workedNephridium
just used these lines setResult(RESULT_OK, null); finish();Nephridium
C
12

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.)

Claustrophobia answered 23/11, 2011 at 13:37 Comment(0)
N
25

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();
Nephridium answered 23/11, 2011 at 11:8 Comment(2)
This is a very complete answerExieexigency
Question: why not use recreate(); in the onActivityResult method?Lycopodium
C
12

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.)

Claustrophobia answered 23/11, 2011 at 13:37 Comment(0)
C
4

You should handle the result of activity that you started with "startActivityForResult" in a parent activity in a method:

@override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
   //...
}

And depending on result you could just invoke again a code that is responsible for showing the information in your parent activity (may be you put it into onResume() method or like that).

I would suggest you to move all the logic responsible for information rendering to a separate method. And invoke it after you recieve the result. Instead of restarting your parent activity.

Crushing answered 23/11, 2011 at 11:0 Comment(1)
just update my answer with the code I'm using but it's not even showing the Log in my onActivityresult.Claustrophobia
M
3

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

Machellemachete answered 23/11, 2011 at 10:59 Comment(0)
K
2

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

Kingofarms answered 23/11, 2011 at 11:3 Comment(0)
H
1

Override onRestart() method inside activity you want to refresh, and recreate the activity

@Override
protected void onRestart() {
    super.onRestart();
    this.recreate();
}
Hazaki answered 18/12, 2019 at 11:36 Comment(0)
S
1

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);
}
Surgy answered 11/10, 2020 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.