Home » Android » android – performing pause of activity that is not resumed after recreate method

android – performing pause of activity that is not resumed after recreate method

Posted by: admin May 14, 2020 Leave a comment

Questions:

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.

How to&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.

Answer:

I don’t know if this is the cause for your problems but you don’t compare Strings like this in Java;

protected void onResume() {
    ...
    if (recreate == "S") {
        recreate = "N";
        recreate();
    }

Use if ("S".equals(recreate)) instead.

Answer:

You should never be calling onPause onCreate onResume etc on your own. You shouldn’t need to use recreate() for what you want to do, put initialisation code elsewhere if it needs updating. Further, use an integer to store the state of the program instead of a string, then declare some final variables to reference e.g.

public final int RECREATE_ON = 1;
public final int RECREATE_OFF = 2;
private int recreate = RECREATE_OFF;

...

if(recreate==RECREATE_ON){
    recreate();
}

Remember what recreate() is doing:

Cause this Activity to be recreated with a new instance. This results
in essentially the same flow as when the Activity is created due to a
configuration change — the current instance will go through its
lifecycle to onDestroy()
and a new instance then created after it.

This is why you are getting the onPause message.