Android error: recreate () must be called from the main thread

I get an Android error message, although the error message is pretty obvious, I can't figure out how to make it work properly.

Error message:

 java.lang.IllegalStateException: Must be called from main thread
        at android.app.Activity.recreate(Activity.java:4193)

My application sends a notification to exit the system (when its token expires).

On older versions of Android, I have no problems, but from SDK 11 and above I have to use the recreate () method. I get an error that it should cause from the main thread.

I moved the recreate () statement to the MainActivity class, this does not work when I call the method from IntentService. I still get the same error. The messaging component is working fine, only processing the exit message leads to this error.

Here are some snippets:

inside GcmIntentService.java

if (logout!=null) {
    VarControl.ma.logout();
}

MainActivity.java

public void logout() {
    deleteToken();
    closeWebView();
    restartApp();
}

public void restartApp() {
    if (Build.VERSION.SDK_INT >= 11) {
        this.recreate(); // THE ERROR OCCURS HERE
    }
    else{
        //left out this part because its not relevant
    }
}

( )?

+4
2

sthg , :

public void restartApp() {
    if (Build.VERSION.SDK_INT >= 11) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                recreate();
            }
        });
    }
    else{
    //left out this part because its not relevant
    }
}
+4

, . , - . , Intent, Runnable List. , . , Runnable . , .

, , .

0

All Articles