Clean or reset screen content (data)

Hello , i want to know how to reset screen when i move to other screen or reset screen content ( for example i have 2 screens , 1 is main and other has spinbox , and how can i reset spinbox data when i move to main screen ?)

void lv_Func(void)
{
    MainScreenFunc();
    Screen2Func();
}

static void MainScreenFunc(void)
{
    /*Main Screen*/
    MainScreen = lv_obj_create(NULL, NULL); /*Create a screen*/
    lv_scr_load(MainScreen);  /*Load the screen*/
    ScreenBackgroung(MainScreen, COLOR_BG_GRAY);

    lv_obj_t * Btn = lv_btn_create(lv_scr_act(), NULL);              /*Create a button on the screen*/
    lv_theme_apply(Btn, (lv_theme_style_t)BUTTON_THEME);
    lv_obj_set_size(Btn, 120, 30);                                   /*Set its size*/
    lv_obj_t * BtnLabel = lv_label_create(Btn, NULL);         /*Add a label to the button*/
    lv_obj_align(Btn, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 20, -10);
    lv_label_set_text(BtnLabel, "BUTTON");                   /*Set the labels text*/
    lv_obj_set_event_cb(Btn, EventCb);
}

static void Screen2Func(void)
{
    Screen2 = lv_obj_create(NULL, NULL);
    ScreenBackgroung(Screen2, COLOR_BG_GRAY);

   lv_obj_t * BackButton = lv_btn_create(Screen2, NULL);
   lv_obj_align(BackButton, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 10);
   lv_obj_set_size(BackButton, 30, 30);
   lv_obj_t * BackButtonLabel = lv_label_create(BackButton, NULL);
   lv_label_set_text(BackButtonLabel, LV_SYMBOL_LEFT);
   lv_obj_set_event_cb(BackButton, BackToMainScreenEventCb);

   Spinbox();
}

LV_EVENT_CB_DECLARE(EventCb)
{
    if (e == LV_EVENT_CLICKED)
    {
        lv_scr_load(Screen2);
    }
}

LV_EVENT_CB_DECLARE(BackToMainScreenEventCb)
{
    if (e == LV_EVENT_CLICKED)
    {
        lv_scr_load(MainScreen);
    }
}

static void lv_spinbox_increment_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(e == LV_EVENT_SHORT_CLICKED || e == LV_EVENT_LONG_PRESSED_REPEAT) {
        lv_spinbox_increment(spinbox1);
    }
}

static void lv_spinbox_decrement_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(e == LV_EVENT_SHORT_CLICKED || e == LV_EVENT_LONG_PRESSED_REPEAT) {
        lv_spinbox_decrement(spinbox1);
    }
}

static void Spinbox(void)
{
    spinbox1 = lv_spinbox_create(Screen2, NULL);
    lv_spinbox_set_range(spinbox1, -100, 900);
    lv_spinbox_set_digit_format(spinbox1, 3, 0);
    lv_spinbox_step_prev(spinbox1);
    lv_obj_set_width(spinbox1, 55);
    lv_obj_align(spinbox1, NULL, LV_ALIGN_IN_TOP_RIGHT, -45, 50);

    lv_coord_t h = lv_obj_get_height(spinbox1);
    lv_obj_t * btn1 = lv_btn_create(Screen2, NULL);
    lv_obj_set_size(btn1, h, h);
    lv_obj_align(btn1, spinbox1, LV_ALIGN_OUT_RIGHT_MID, 1, 0);
    lv_theme_apply(btn1, LV_THEME_SPINBOX_BTN);
    lv_obj_set_style_local_value_str(btn1, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_SYMBOL_PLUS);
    lv_obj_set_event_cb(btn1, lv_spinbox_increment_event_cb);

    btn1 = lv_btn_create(Screen2, btn1);
    lv_obj_align(btn1, spinbox1, LV_ALIGN_OUT_LEFT_MID, -1, 0);
    lv_obj_set_event_cb(btn1, lv_spinbox_decrement_event_cb);
    lv_obj_set_style_local_value_str(btn1, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_SYMBOL_MINUS);
}

For example (2 screens , 1 with button and 2 with backbutton and spinbox ) , and how do i need reset spinbox data (content) or the whole screen2 , in such way so if i click back button , i go to main screen and in same time spinbox data (digits) is cleared , and when i again click button on main screen i go to screen2 where is back button and spinbox with cleared data ( with 000 ). Because when i try lv_obj_clean(Screen2); it clean all but when i again go to screen2 it is empty. (what i doing wrong =))), P.S Is it correct how i do transition between 2 screens ? , or exist better way ?)

You can call lv_obj_clean(lv_scr_act()) to remove all objects from your screen and get a “clean slate”.

I tried this , and when i try again enter 2 screen it is empty

share the code snippet. it is needed to suggest you a solution

1 Like
void lv_Func(void)
{
    MainScreenFunc();
    Screen2Func();
}

static void MainScreenFunc(void)
{
    /*Main Screen*/
    MainScreen = lv_obj_create(NULL, NULL); /*Create a screen*/
    lv_scr_load(MainScreen);  /*Load the screen*/
    ScreenBackgroung(MainScreen, COLOR_BG_GRAY);

    lv_obj_t * Btn = lv_btn_create(lv_scr_act(), NULL);              /*Create a button on the screen*/
    lv_theme_apply(Btn, (lv_theme_style_t)BUTTON_THEME);
    lv_obj_set_size(Btn, 120, 30);                                   /*Set its size*/
    lv_obj_t * BtnLabel = lv_label_create(Btn, NULL);         /*Add a label to the button*/
    lv_obj_align(Btn, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 20, -10);
    lv_label_set_text(BtnLabel, "BUTTON");                   /*Set the labels text*/
    lv_obj_set_event_cb(Btn, EventCb);
}

static void Screen2Func(void)
{
    Screen2 = lv_obj_create(NULL, NULL);
    ScreenBackgroung(Screen2, COLOR_BG_GRAY);

   lv_obj_t * BackButton = lv_btn_create(Screen2, NULL);
   lv_obj_align(BackButton, NULL, LV_ALIGN_IN_TOP_LEFT, 10, 10);
   lv_obj_set_size(BackButton, 30, 30);
   lv_obj_t * BackButtonLabel = lv_label_create(BackButton, NULL);
   lv_label_set_text(BackButtonLabel, LV_SYMBOL_LEFT);
   lv_obj_set_event_cb(BackButton, BackToMainScreenEventCb);

   Spinbox();
}

LV_EVENT_CB_DECLARE(EventCb)
{
    if (e == LV_EVENT_CLICKED)
    {
        lv_scr_load(Screen2);
    }
}

LV_EVENT_CB_DECLARE(BackToMainScreenEventCb)
{
    if (e == LV_EVENT_CLICKED)
    {
        lv_scr_load(MainScreen);
    }
}

static void lv_spinbox_increment_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(e == LV_EVENT_SHORT_CLICKED || e == LV_EVENT_LONG_PRESSED_REPEAT) {
        lv_spinbox_increment(spinbox1);
    }
}

static void lv_spinbox_decrement_event_cb(lv_obj_t * obj, lv_event_t e)
{
    if(e == LV_EVENT_SHORT_CLICKED || e == LV_EVENT_LONG_PRESSED_REPEAT) {
        lv_spinbox_decrement(spinbox1);
    }
}

static void Spinbox(void)
{
    spinbox1 = lv_spinbox_create(Screen2, NULL);
    lv_spinbox_set_range(spinbox1, -100, 900);
    lv_spinbox_set_digit_format(spinbox1, 3, 0);
    lv_spinbox_step_prev(spinbox1);
    lv_obj_set_width(spinbox1, 55);
    lv_obj_align(spinbox1, NULL, LV_ALIGN_IN_TOP_RIGHT, -45, 50);

    lv_coord_t h = lv_obj_get_height(spinbox1);
    lv_obj_t * btn1 = lv_btn_create(Screen2, NULL);
    lv_obj_set_size(btn1, h, h);
    lv_obj_align(btn1, spinbox1, LV_ALIGN_OUT_RIGHT_MID, 1, 0);
    lv_theme_apply(btn1, LV_THEME_SPINBOX_BTN);
    lv_obj_set_style_local_value_str(btn1, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_SYMBOL_PLUS);
    lv_obj_set_event_cb(btn1, lv_spinbox_increment_event_cb);

    btn1 = lv_btn_create(Screen2, btn1);
    lv_obj_align(btn1, spinbox1, LV_ALIGN_OUT_LEFT_MID, -1, 0);
    lv_obj_set_event_cb(btn1, lv_spinbox_decrement_event_cb);
    lv_obj_set_style_local_value_str(btn1, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_SYMBOL_MINUS);
}

For example (2 screens , 1 with button and 2 with backbutton and spinbox ) , and how do i need reset spinbox data (content) or the whole screen2 , in such way so if i click back button , i go to main screen and in same time spinbox data (digits) is cleared , and when i again click button on main screen i go to screen2 where is back button and spinbox with cleared data ( with 000 ). Because when i try lv_obj_clean(Screen2); it clean all but when i again go to screen2 it is empty. (what i doing wrong =))), P.S Is it correct how i do transition between 2 screens ? , or exist better way ?)