我需要能够改变一些变量的值,当应用程序关闭。我使用的是这里描述的exitEvent:https://docs.nativescript.org/core-concepts/application-lifecycle
另外,我使用的是本地存储插件,它的工作原理类似于javasctip的本地存储。https://github.com/NathanaelA/nativescript-localstorage
我的简单代码如下所示:
var Observable = require("data/observable").Observable;
require("nativescript-dom");
var LS = require("nativescript-localstorage");
const application = require("tns-core-modules/application");
var page = require("ui/page");
exports.load = function (args) {
var page = args.object;
var model = new Observable();
var frame = require('ui/frame');
var ff = frame.getFrameById("dashboard");
var topmost = frame.topmost();
// This is exit event
application.on(application.exitEvent, (args) => {
LS.setItem('XX','111')
});
// What i will exit app, and run app again this will newer become 111,
it's null all the time
console.log(LS.getItem('XX'));
}所以,我的问题是-是否可以在应用程序退出时设置任何标志-它不必是本地存储(我已经测试了全局变量),以检测是否退出,并基于此我可以做出对我有帮助的决定?
其中一种情况可能是-我在本地存储中持有一些标志,即用户在登录屏幕上点击了"rememebr me“。因此,在退出时,我可以检查他是否希望被记住,如果不是,我想在应用程序启动时将用户发送到登录页面而不是仪表板。
谢谢。
编辑:我也尝试过应用程序设置,它不会工作。
application.on(application.exitEvent, (args) => {
applicationSettings.setString("Name", "John Doe");
});
console.log(applicationSettings.getString("Name")); // not working发布于 2019-02-23 20:04:39
我怀疑这是nativescript-localstorage插件的问题。它将更改写入文件after a 250ms delay。在退出事件中,在你的应用程序被系统完全杀死之前,你只有很有限的时间。
也许作者设置这个延迟是有原因的,但我认为它的时间太长了,至少在这个特定的场景中,所以更改永远不会写到文件中。你可以在插件的Github repo上提出一个问题。
如果您正在寻找一种即时的解决方法,请将localstorage.js复制到您的应用程序中并从文件中导出internalSaveData,这样您就可以在完成设置值后立即直接调用它。
https://stackoverflow.com/questions/54840582
复制相似问题