Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

performing pause of activity that is not resumed after recreate method

I have a project for HoneyComb and I get an error after use recreate() method at onResum() method in my main Activity.

11-10 22:05:42.090: E/ActivityThread(1917): Performing pause of activity that is not     resumed: {com.blogspot.honeyapp/com.blogspot.honeyapp.Main}
11-10 22:05:42.090: E/ActivityThread(1917): java.lang.RuntimeException: Performing pause of activity that is not resumed: {com.blogspot.honeyapp/com.blogspot.honeyapp.Main}
11-10 22:05:42.090: E/ActivityThread(1917):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2517)
11-10 22:05:42.090: E/ActivityThread(1917):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2505)
11-10 22:05:42.090: E/ActivityThread(1917):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2483)
11-10 22:05:42.090: E/ActivityThread(1917):     at android.app.ActivityThread.access$700(ActivityThread.java:122)
11-10 22:05:42.090: E/ActivityThread(1917):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1031)
11-10 22:05:42.090: E/ActivityThread(1917):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 22:05:42.090: E/ActivityThread(1917):     at android.os.Looper.loop(Looper.java:132)
11-10 22:05:42.090: E/ActivityThread(1917):     at android.app.ActivityThread.main(ActivityThread.java:4123)
11-10 22:05:42.090: E/ActivityThread(1917):     at java.lang.reflect.Method.invokeNative(Native Method)
11-10 22:05:42.090: E/ActivityThread(1917):     at java.lang.reflect.Method.invoke(Method.java:491)
11-10 22:05:42.090: E/ActivityThread(1917):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-10 22:05:42.090: E/ActivityThread(1917):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-10 22:05:42.090: E/ActivityThread(1917):     at dalvik.system.NativeStart.main(Native Method)

I create a new project to show you what's happen.

You can find it at http://xp-dev.com/svn/RecreateError/trunk/

I don't know what's my fault but I start Activity and log the Activity's lifecycle. The result:

11-10 22:26:45.960: I/seasons log(2274): onCreate()
11-10 22:26:45.990: I/seasons log(2274): onStart()
11-10 22:26:45.990: I/seasons log(2274): onResume()

Now I press the Action Bar Icon to activate the recreate flag and change to other app...

11-10 22:30:26.390: I/seasons log(2274): onPause()
11-10 22:30:27.080: I/seasons log(2274): onStop()

And return to my Activity with recreate flag activated what will done recreate() at onResume().

11-10 22:33:05.500: I/seasons log(2274): onCreate()
11-10 22:33:05.510: I/seasons log(2274): onStart()
11-10 22:33:05.510: I/seasons log(2274): onResume()
11-10 22:33:05.510: I/seasons log(2274): onPause()

onPause? But my Activity is visible, what I'm doing wrong? The correct status aren't onResume()?

And now if I change to another app I get the error.

Thanks for your time and sorry for my bad English.


At this time I don't understand how applications like File Manager HD do this action.

Two Activities: Main Activity A, Activity B with PreferenceFragment as main content.

One option that changes theme between Holo and Holo.Light, Activity B changes with a OnSharedPreferenceChangeListener method in PreferenceFragment but when we come back to the Main Activity recreate() method at onResume() fails, how to do This?

I'm confussed. Sorry.

like image 740
seasonsend Avatar asked Nov 10 '11 21:11

seasonsend


1 Answers

To do this, use a handler:

Handler handler = new Handler() {
       @Override
        public void handleMessage(Message msg) {
           if(msg.what==MSG_RECREATE)
               recreate();
        }
};

@Override
protected void onResume() {
    if(condition) {
        Message msg = handler.obtainMessage();
        msg.what = MSG_RECREATE;
        handler.sendMessage(msg);
    }
}

This will not crash anymore.

like image 74
Philippe Mignard Avatar answered Nov 06 '22 13:11

Philippe Mignard