I have an application and there are certain conditions when I want my activity to be recreated or the onCreate
function is needed to be called so that different functions can be performed again. Just like when the orientation of the device changes and the oncreate function is recalled or the activity is recreated, in the same way, I want my application to be restarted. Currently I am using this.onCreate(null)
but I think this is not the best way..
Please give some suggestions.
Thanks alot
Asked
Active
Viewed 2.2k times
7

Quintin Robinson
- 81,193
- 14
- 123
- 132

Farhan
- 3,206
- 14
- 49
- 62
-
Hi. Could you give a bit more context. When do you need to restart and why? – Pikaling Aug 22 '11 at 15:46
-
actually, i give the options to the user to select whether they want to use internal storage or external. whenever they change their choice, i restart the activity using this.onCreate(null) so that all the checks that are performed on the storage can be performed again.. Is this approach correct? – Farhan Aug 22 '11 at 15:54
1 Answers
20
How about creating a method outside of your onCreate() that does all of the Activities work, and in your onCreate method, it calls that to load the Activity. If you need to refresh your Activity, just call that new method. For example:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadActivity();
}
private void loadActivity() {
// Do all of your work here
}
private OnClickListener ReloadActivity = new OnClickListener() {
public void onClick(View v) {
loadActivity();
}
};

hooked82
- 6,336
- 4
- 41
- 46
-
and please tell me what would make the difference in calling this.onCreate(null) and loadActivity(). what would be the difference?? – Farhan Aug 22 '11 at 16:23
-
This all depends on how you handle it and what is done in your loadActivity(). I would highly suggest against this.onCreate(null), as I don't know the repercussions of doing so. Having a method called in your onCreate and invoking it from a button, etc. is the suggest way to handle this. – hooked82 Aug 22 '11 at 16:37
-
Sir, I'll try your solution. Can you please help me in understanding what difference it would make if I call loadActivity() instead of onCreate(null).. Just want to know the difference? – Farhan Aug 22 '11 at 17:44