performing pause of activity that is not resumed after recreate method

五迷三道 提交于 2019-12-04 01:25:06

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!