restart an app with code
96 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I am making an app using App Designer. Is there any way to close and re-open (reboot) the app with a pushbottun callback in the app?
Any advise would be appreciated. Thank you!
1 comentario
Guillaume
el 30 de Jun. de 2019
No that's not possible, but that sounds like a very strange thing to want to do. Can you describe your use case so we can suggest a better solution.
Respuestas (3)
Adam Danz
el 1 de Jul. de 2019
Editada: Adam Danz
el 1 de Jul. de 2019
I agree with Guillaume that closing and reopening an app might be a red flag that something is being done suboptimally (or worse). If you're trying to reset the app so that all components are set to their default values, you should develop a callback function that does that instead of closing and reopening the app.
That being said, here's how you can close the app and open a new instance if that's what you need to do. This callback function makes the currently running instance invisible, opens a second instance with default values, and then deletes the first one.
% Button pushed function: Button
function ButtonPushed(app, event)
% Make current instance of app invisible
app.UIFigure.Visible = 'off';
% Open 2nd instance of app
myFakeApp(); % <--------------The name of your app
% Delete old instance
close(app.UIFigure) %Thanks to Guillaume for suggesting to use close() rather than delete()
end
Note this creates a whole new set of handles to the newly opened app so if any external code interacts with the app and requires access to its handles, those handles will have changed.
8 comentarios
Hope Walker
el 12 de Feb. de 2020
Hey AT, I was struggling with this same issue for quite a while but I figured out a super easy way that seems to work really well (I haven't had any issues with it so far). All I did was set my startup callback function to how I wanted the app to be when I started and then called that function when a button was pressed.
% Button pushed function: NewQuizButton
function NewQuizButtonPushed(app, event)
startupFcn(app)
end
This seems to avoid having to reset all variables or close and reopen the app while still doing the same thing. Hopefully this helps!
2 comentarios
Guillaume
el 12 de Feb. de 2020
This is exactly what I suggested to do in my comment above: "You would be better off putting all your UI initialisation code in the StatupFcn and calling that every time you want to reset the app. This would avoid needlessly destroying a bunch of control to then recreate them."
Indeed, this is a much better approach.
Adam Danz
el 12 de Feb. de 2020
Editada: Adam Danz
el 12 de Feb. de 2020
I agree this is a more efficient approach than closing/re-opening the app but it also requires more responsibility and attention to detail. The big difference between the two approaches is that in the StartupFcn approach, you must very carefully make sure all components of the app are addressed in the StartupFcn including clearing/resetting the custom property values declared within the app. If a new component is added to the app and you forget to add this component's default value in the startupFcn, it would potentially go unnoticed. Another example would be the use of setappdata where data are stored in the app but not cleared upon reset.
The close-and-reopen method avoids these problems. So, there's a tradeoff to consider.
Xiaohao Guo
el 24 de Ag. de 2023
I found it works by creating a temp object defined as follows:
classdef tempObjectForRestarting < handle
properties
app (1,1) appMain
end
methods
function obj = tempObjectForRestarting(app)
obj.app = app;
end
function closeMainApp(obj)
clf;
close all;
delete(obj.app);
obj.app = appMain();
delete(obj);
end
end
end
And in the main app, i use following two lines for restarting:
tempObject = tempObjectForRestarting(app);
closeMainApp(tempObject)
1 comentario
Ver también
Categorías
Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!