How to close parent win when crean a new win

Description

a win have a button when i click it, it creat a new win, at the sametime i want to close the parent win how to?

What MCU/Processor/Board and compiler are you using?

HiSilicon ARM

What do you want to achieve?

What have you tried so far?

at lvgl 5.3 i call this function is ok,but at 6.0 is segment fault

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

static lv_res_t close_former_win(lv_obj_t * btn)
{
    lv_obj_t * btnwin; 
    printf("btn %p\n",btn);
    printf("Press\n");
    lv_obj_t * win = lv_win_get_from_btn(btn);
    btnwin = lv_obj_get_parent(win);
    printf("the win title is %s\n",lv_win_get_title(btnwin));

    lv_obj_del(win);    
    lv_obj_del(btnwin);
    return LV_RES_OK;
}

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

You are trying to delete a window while still working with the window objects.
Try using lv_async_call to postpone deletion of the window until the next lvgl update.
Check out documentation.

For v6 it should look something like:

static void delete_later(void * obj){
    lv_obj_del( (lv_obj_t *) obj);
}

static void close_former_win(lv_obj_t * btn, lv_event_t event){
    if(event == LV_EVENT_RELEASED){
        // your old code
        lv_async_call(delete_later, win);
        lv_async_call(delete_later, btnwin);
    }
}

You could just use lv_obj_del_async.

1 Like

the fallowing way is ok
by the way at lvgl 6.0 you should recheck the event

static void close_former_win(lv_obj_t * btn, lv_event_t event)
{
    lv_obj_t * btnwin; 
    if(event == LV_EVENT_RELEASED)
    {
        lv_obj_t * win = lv_win_get_from_btn(btn);
        btnwin = lv_obj_get_parent(win);
        printf("the win title is %s\n",lv_win_get_title(btnwin));
        lv_obj_del_async(btnwin);
    }

    return LV_RES_OK;
}

You don’t need to return. It’s a void function.