performing pause of activity that is not resumed after recreate method

后端 未结 3 1554
刺人心
刺人心 2021-02-20 09:26

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)         


        
相关标签:
3条回答
  • 2021-02-20 10:05

    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.

    0 讨论(0)
  • 2021-02-20 10:18

    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.

    0 讨论(0)
  • 2021-02-20 10:30

    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.

    0 讨论(0)
提交回复
热议问题