views:

903

answers:

5

Is it a good practice to reload an activity in Android?

What would be the best way to do it? this.finish and then this.startActivity with the activity intent?

A: 

I don't think that's a good idea... it'd be better to implement a cleaner method. For instance, if your activity holds a form, the cleaner method could just clear each widget in the form and delete all temporary data. I guess that's what you want: restore the activity to its initial state.

Cristian
Actually, I want to change the application state and then reload the activity to reflect the change.
hgpc
What is the "application state" in your case? Preferences?
zehrer
Yes, preferences.
hgpc
A: 

in some cases it's the best practice in other it's not a good idea it's context driven if you chose to do so using the falowing is the best way to pass from an activity to her sons :

    Intent i = new Intent(myCurrentActivityName.this,activityIWishToRun.class);    
    startActivityForResult(i, GlobalDataStore.STATIC_INTEGER_VALUE);

the thing is whenever you finish() from activityIWishToRun you return to your a living activity

yoav.str
+1  A: 

Android includes a process management system which handles the creation and destruction of activities which largely negates any benefit you'd see from manually restarting an activity. You can see more information about it at http://developer.android.com/guide/topics/fundamentals.html#procthread

What is good practice though is to ensure that your onPause and onStop methods release any resources which you don't need to hold on to and use onLowMemory (http://developer.android.com/reference/android/app/Activity.html#onLowMemory()) to reduce your activities needs to the absolute minimum.

Al Sutton
+1 for mentioning onLowMemory. I didn't know that such method existed!
hgpc
A: 

I want to refresh the activity then I get a new location or random number (nothing to do with the best way of closing etc..)

It does it anyway when the orientation is changed so why not have a method to run?

Its probably obvious but I cannot find it.....

Droid
Welcome to stackoverflow. You should ask a question, not reply to one.
hgpc
A: 

After experimenting with this for a while I've found no unexpected consequences of restarting an activity. Also, I believe this is very similar to what Android does by default when the orientation changes, so I don't see a reason not to do it in a similar circumstance.

hgpc
On the contrary, I tried to "refresh" my screen by restarting the activity. Itdoesn't appear on the surface anything went wrong, my information updated the how i wanted, the LogCat tells a different story.In LogCat, I go back into the onCreate method of the activity, it attempts to pull preferences. 1st attempt I receive a null pointer exception, then it attempts again to start in the onCreate and receives the storedPref the 2nd time through. Not sure what is going on here, but I just wanted to be sure that you weren't simply looking at the outcome, for WYSIWIG was not the case for me.
taraloca
How exactly are you restarting the activity?
hgpc