Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart the onCreate function

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

like image 596
Farhan Avatar asked Aug 22 '11 15:08

Farhan


People also ask

Is onCreate only called once?

@OnCreate is only for initial creation, and thus should only be called once. If you have any processing you wish to complete multiple times you should put it elsewhere, perhaps in the @OnResume method.

How do I restart my kotlin activity?

This example demonstrates how to restart an Android Activity using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.

How many times does onCreate run?

You might want to read through the documentation on the Activity lifecycle. OnCreate will only be called one time for each lifetime of the Activity. However, there are a number of situations that can cause your activity to be killed and brought back to life. Thus, onCreate will be called again.


1 Answers

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();
    }
};
like image 180
hooked82 Avatar answered Oct 05 '22 08:10

hooked82