Why does the "StartupFcn" only run once for Single Running Instance apps?

I am in the process of migrating a GUIDE app to App Designer. I had had my GUIDE app set to only allow a single running instance (singleton), but its "OpeningFcn" would still execute every time I called the app, even if it was already open. After migrating, the "StartupFcn" of my single running instance App Designer app only executes once. I am using MATLAB R2020b or later, which is when the "Single Running Instance" was added.
Is this behavior intended, and is there any way to execute the "StartupFcn" for my single running instance App Designer app more than once?

 Accepted Answer

The intended behavior is that running an open singleton App Designer app will cause the app to come to front and not re-execute the "StartupFcn". The "StartupFcn" is intended to only be executed once, the first time each instance of the app is run.
As a workaround, try using two apps, where the first app contains a callback method to open and run the second app. If the second app does not currently exist, the callback should instantiate it, then execute a public method of the second app. This public method should make the second app visible and utilize "uiwait" to pause execution until you are done working with the second app. Add a "CloseRequestFcn" to the second app's "UIFigure" that utilizes "uiresume" and turns off the visibility of the second app. This returns execution to the first app, which you can work with until you want to relaunch the second app, which will be just as you left it. See the below code snippet for guidance.
%% MainApp.mlapp % Include "SecondApp" as an app property function OpenSecondAppButtonPushed(app, event) if isempty(app.SecondApp) || ~isvalid(app.SecondApp) % runs the first time this callback is executed app.SecondApp = SecondaryApp(); end % pass execution to SecondApp app.SecondApp.makeVisibleAndWait(); end function UIFigureCloseRequest(app, event) delete(app.SecondApp); delete(app); end
%% SecondaryApp.mlapp function makeVisibleAndWait(app) % public method app.UIFigure.Visible = 'on'; % pause execution while working with the second app uiwait(app.UIFigure); end function UIFigureCloseRequest(app, event) % return execution uiresume(app.UIFigure) app.UIFigure.Visible = 'off'; end
For more information, read about the "Single Running Instance" option of App Designer apps, "uiwait", and "uiresume".

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products

Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!