首页 \ 问答 \ onStop未被调用...按下主页按钮(onStop not being called … Home button pressed)

onStop未被调用...按下主页按钮(onStop not being called … Home button pressed)

在我的主要活动中,在onStop下,我设置myVar = true。 在onResume中,我检查myVar是否为true并执行某些操作。

如果您在我的主要活动中点击主页按钮并从主屏幕/应用程序抽屉再次启动它,它可以正常工作。

如果你在一个不同的活动中点击主页按钮并从主屏幕/应用程序抽屉再次启动它,它会显示主要活动,因为我在我的主要活动上设置了clearTaskOnLaunch =“true”并且android:finishOnTaskLaunch =“true”我的其他活动。 但是,当按下Home按钮时,它似乎没有在主活动中点击onStop。

我开始其他活动的结果。 如果结果代码= result_ok或result_canceled,我设置myVar = false。 但是......如果按下主页按钮,则不应该设置结果并执行onActivityResult。

知道怎么解决这个问题吗?

编辑:

above onCreate.. I set startNew = true;
@Override
    public void onStop() {
        super.onStop();
        startNew = true;
    }

    @Override
    public void onRestart() {
        super.onRestart();
        if (startNew) {
            getCurrentDate(0);
            updateDisplay();
            fillData();
        }
    }

对startNew的唯一其他引用是在我的onActivityResult方法中,如果从我的主活动启动的活动返回result_ok或result_canceled,我设置startNew = false。

我试图确保每次我的应用程序从主屏幕/应用程序抽屉启动,它运行这3种方法。


In my main activity, under onStop, I set myVar = true. In onResume, I check if myVar = true and do something.

If you hit the home button while on my main activity and launch it again from the home screen/app drawer, it works correctly.

If you hit the home button while on a different activity and launch it again from the home screen/app drawer, it brings up the main activity as I have clearTaskOnLaunch="true" set on my main activity and android:finishOnTaskLaunch="true" set on my other activities. However, it doesn't appear that it hit onStop in the main activity when the Home button was pressed.

I start the other activities for result. If result code = result_ok or result_canceled, I set myVar = false. But... if the home button is pressed, it shouldn't be setting results and doing the onActivityResult.

Any idea how to solve this?

Edit:

above onCreate.. I set startNew = true;
@Override
    public void onStop() {
        super.onStop();
        startNew = true;
    }

    @Override
    public void onRestart() {
        super.onRestart();
        if (startNew) {
            getCurrentDate(0);
            updateDisplay();
            fillData();
        }
    }

The only other reference to startNew, is in my onActivityResult method, if an activity that was launched from my main activity returns either result_ok or result_canceled, I set startNew = false.

I'm trying to ensure that every time my app is launched from home screen / app drawer, that it runs those 3 methods.


原文:https://stackoverflow.com/questions/5625428
更新时间:2022-01-20 20:01

最满意答案

根据我的经验,将财务数据保留为xts对象通常会更有意义,以便与其他技术指标等进行日后操作,除非您计划在caret中运行预测模型,在这种情况下转换为data.frame可能会使感。

考虑将数据的符号保存为容器的元素,例如

update_sym_md <- function(sym, env = .GlobalEnv) {
    x <- get(sym, env)
    pd <- setNames((Hi(x) - Op(x)) / Op(x), "PD")
    merge(x, pd)
}

# Adjust env for location of xts symbol data
l.syms <- lapply(Nasdaq100_Symbols, update_sym_md, env = .GlobalEnv)

lapply(l.syms, head)
# [[1]]
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted          PD
# 2007-01-03     86.29     86.58    81.90      83.80   309579900      10.85709 0.003360760
# 2007-01-04     84.05     85.95    83.82      85.66   211815100      11.09807 0.022605556
# 2007-01-05     85.77     86.20    84.40      85.05   208685400      11.01904 0.005013373
# 2007-01-08     85.96     86.53    85.28      85.47   199276700      11.07345 0.006630991
# 2007-01-09     86.45     92.98    85.15      92.57   837324600      11.99333 0.075534942
# 2007-01-10     94.75     97.80    93.45      97.00   738220000      12.56728 0.032190006
# 
# [[2]]
# AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted           PD
# 2007-01-03    53.89    56.92   53.89     56.30    2955600     54.80361 0.0562256273
# 2007-01-04    56.30    59.15   53.65     58.84    2614500     57.27610 0.0506217238
# 2007-01-05    58.83    59.15   57.90     58.29    1656300     56.74072 0.0054394015
# 2007-01-08    57.30    60.48   57.04     57.93    2163200     56.39028 0.0554974006
# 2007-01-09    59.44    60.20   57.56     57.90    2098600     56.36108 0.0127860366
# 2007-01-10    60.03    60.04   57.34     58.93    3892200     57.36371 0.0001666167

另外,如果您想要比较一个xts对象中的符号中的价格回报/原始价格,而不是数据框架中的价格回报/原始价格,则可能会发现qmao包有用。

例如:

install.packages("qmao", repos="http://R-Forge.R-project.org", type = "source")
library(qmao)

pf <- makePriceFrame(Nasdaq100_Symbols)
head(pf, 3)
#               AAPL      AAL
# 2007-01-03 10.85709 54.80361
# 2007-01-04 11.09807 57.27610
# 2007-01-05 11.01904 56.74072
rf <- makeReturnFrame(Nasdaq100_Symbols)
head(rf)

#                  AAPL           AAL
# 2007-01-03           NA            NA
# 2007-01-04  0.021952895  0.0441273684
# 2007-01-05 -0.007146715 -0.0093913155
# 2007-01-08  0.004926208 -0.0061951917
# 2007-01-09  0.079799692 -0.0005179716
# 2007-01-10  0.046745798  0.0176329011

根据OP发表的意见更新:

要将所有数据合并到一行中,请尝试以下操作:

(另外:如果您要在此数据框架上使用非线性预测模型,请确保您考虑首先在每行中的证券上缩放数据点。)

x.cbind <- do.call(cbind, l.syms)
head(x.cbind)
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted          PD AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted         PD.1
# 2007-01-03     86.29     86.58    81.90      83.80   309579900      10.85709 0.003360760    53.89    56.92   53.89     56.30    2955600     54.80361 0.0562256273
# 2007-01-04     84.05     85.95    83.82      85.66   211815100      11.09807 0.022605556    56.30    59.15   53.65     58.84    2614500     57.27610 0.0506217238
# 2007-01-05     85.77     86.20    84.40      85.05   208685400      11.01904 0.005013373    58.83    59.15   57.90     58.29    1656300     56.74072 0.0054394015
# 2007-01-08     85.96     86.53    85.28      85.47   199276700      11.07345 0.006630991    57.30    60.48   57.04     57.93    2163200     56.39028 0.0554974006
# 2007-01-09     86.45     92.98    85.15      92.57   837324600      11.99333 0.075534942    59.44    60.20   57.56     57.90    2098600     56.36108 0.0127860366
# 2007-01-10     94.75     97.80    93.45      97.00   738220000      12.56728 0.032190006    60.03    60.04   57.34     58.93    3892200     57.36371 0.0001666167

df.cbind <- data.frame("time" = index(x.cbind), coredata(x.cbind))
head(df.cbind)
# time AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted          PD AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted         PD.1
# 1 2007-01-03     86.29     86.58    81.90      83.80   309579900      10.85709 0.003360760    53.89    56.92   53.89     56.30    2955600     54.80361 0.0562256273
# 2 2007-01-04     84.05     85.95    83.82      85.66   211815100      11.09807 0.022605556    56.30    59.15   53.65     58.84    2614500     57.27610 0.0506217238
# 3 2007-01-05     85.77     86.20    84.40      85.05   208685400      11.01904 0.005013373    58.83    59.15   57.90     58.29    1656300     56.74072 0.0054394015
# 4 2007-01-08     85.96     86.53    85.28      85.47   199276700      11.07345 0.006630991    57.30    60.48   57.04     57.93    2163200     56.39028 0.0554974006
# 5 2007-01-09     86.45     92.98    85.15      92.57   837324600      11.99333 0.075534942    59.44    60.20   57.56     57.90    2098600     56.36108 0.0127860366
# 6 2007-01-10     94.75     97.80    93.45      97.00   738220000      12.56728 0.032190006    60.03    60.04   57.34     58.93    3892200     57.36371 0.0001666167

为了更好地理解qmao函数是如何工作的,为什么不看一下文档中的例子并从那里出发? ?makeReturnFrame查看源代码以真正理解正在发生的事情(通过学习良好的编码风格,同时成为更好的R程序员)


In my experience, it usually makes much more sense to keep your financial data as xts objects, for future manipulation with other technical indicators etc, unless you plan to run a prediction model in say caret in which case converting to a data.frame may make sense.

Consider keeping your symbols of data as elements of a container, such as

update_sym_md <- function(sym, env = .GlobalEnv) {
    x <- get(sym, env)
    pd <- setNames((Hi(x) - Op(x)) / Op(x), "PD")
    merge(x, pd)
}

# Adjust env for location of xts symbol data
l.syms <- lapply(Nasdaq100_Symbols, update_sym_md, env = .GlobalEnv)

lapply(l.syms, head)
# [[1]]
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted          PD
# 2007-01-03     86.29     86.58    81.90      83.80   309579900      10.85709 0.003360760
# 2007-01-04     84.05     85.95    83.82      85.66   211815100      11.09807 0.022605556
# 2007-01-05     85.77     86.20    84.40      85.05   208685400      11.01904 0.005013373
# 2007-01-08     85.96     86.53    85.28      85.47   199276700      11.07345 0.006630991
# 2007-01-09     86.45     92.98    85.15      92.57   837324600      11.99333 0.075534942
# 2007-01-10     94.75     97.80    93.45      97.00   738220000      12.56728 0.032190006
# 
# [[2]]
# AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted           PD
# 2007-01-03    53.89    56.92   53.89     56.30    2955600     54.80361 0.0562256273
# 2007-01-04    56.30    59.15   53.65     58.84    2614500     57.27610 0.0506217238
# 2007-01-05    58.83    59.15   57.90     58.29    1656300     56.74072 0.0054394015
# 2007-01-08    57.30    60.48   57.04     57.93    2163200     56.39028 0.0554974006
# 2007-01-09    59.44    60.20   57.56     57.90    2098600     56.36108 0.0127860366
# 2007-01-10    60.03    60.04   57.34     58.93    3892200     57.36371 0.0001666167

Also, if you do want to compare price returns/raw prices across symbols in one xts object, rather than in a data.frame, you might find the qmao package useful.

For example:

install.packages("qmao", repos="http://R-Forge.R-project.org", type = "source")
library(qmao)

pf <- makePriceFrame(Nasdaq100_Symbols)
head(pf, 3)
#               AAPL      AAL
# 2007-01-03 10.85709 54.80361
# 2007-01-04 11.09807 57.27610
# 2007-01-05 11.01904 56.74072
rf <- makeReturnFrame(Nasdaq100_Symbols)
head(rf)

#                  AAPL           AAL
# 2007-01-03           NA            NA
# 2007-01-04  0.021952895  0.0441273684
# 2007-01-05 -0.007146715 -0.0093913155
# 2007-01-08  0.004926208 -0.0061951917
# 2007-01-09  0.079799692 -0.0005179716
# 2007-01-10  0.046745798  0.0176329011

Update in response to comment from OP:

To join all the data into one row, try this:

(Aside: if you're going to use non linear prediction models on this data.frame, make sure you consider scaling your data points across securities in each row first.)

x.cbind <- do.call(cbind, l.syms)
head(x.cbind)
# AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted          PD AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted         PD.1
# 2007-01-03     86.29     86.58    81.90      83.80   309579900      10.85709 0.003360760    53.89    56.92   53.89     56.30    2955600     54.80361 0.0562256273
# 2007-01-04     84.05     85.95    83.82      85.66   211815100      11.09807 0.022605556    56.30    59.15   53.65     58.84    2614500     57.27610 0.0506217238
# 2007-01-05     85.77     86.20    84.40      85.05   208685400      11.01904 0.005013373    58.83    59.15   57.90     58.29    1656300     56.74072 0.0054394015
# 2007-01-08     85.96     86.53    85.28      85.47   199276700      11.07345 0.006630991    57.30    60.48   57.04     57.93    2163200     56.39028 0.0554974006
# 2007-01-09     86.45     92.98    85.15      92.57   837324600      11.99333 0.075534942    59.44    60.20   57.56     57.90    2098600     56.36108 0.0127860366
# 2007-01-10     94.75     97.80    93.45      97.00   738220000      12.56728 0.032190006    60.03    60.04   57.34     58.93    3892200     57.36371 0.0001666167

df.cbind <- data.frame("time" = index(x.cbind), coredata(x.cbind))
head(df.cbind)
# time AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted          PD AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted         PD.1
# 1 2007-01-03     86.29     86.58    81.90      83.80   309579900      10.85709 0.003360760    53.89    56.92   53.89     56.30    2955600     54.80361 0.0562256273
# 2 2007-01-04     84.05     85.95    83.82      85.66   211815100      11.09807 0.022605556    56.30    59.15   53.65     58.84    2614500     57.27610 0.0506217238
# 3 2007-01-05     85.77     86.20    84.40      85.05   208685400      11.01904 0.005013373    58.83    59.15   57.90     58.29    1656300     56.74072 0.0054394015
# 4 2007-01-08     85.96     86.53    85.28      85.47   199276700      11.07345 0.006630991    57.30    60.48   57.04     57.93    2163200     56.39028 0.0554974006
# 5 2007-01-09     86.45     92.98    85.15      92.57   837324600      11.99333 0.075534942    59.44    60.20   57.56     57.90    2098600     56.36108 0.0127860366
# 6 2007-01-10     94.75     97.80    93.45      97.00   738220000      12.56728 0.032190006    60.03    60.04   57.34     58.93    3892200     57.36371 0.0001666167

To better understand how qmao functions work, why not look at the examples in the documentation and go from there? ?makeReturnFrame Look at the source code to really understand what's going on (and become a better R programmer at the same time by learning good coding styles)

相关问答

更多
  • 根据我的经验,将财务数据保留为xts对象通常会更有意义,以便与其他技术指标等进行日后操作,除非您计划在caret中运行预测模型,在这种情况下转换为data.frame可能会使感。 考虑将数据的符号保存为容器的元素,例如 update_sym_md <- function(sym, env = .GlobalEnv) { x <- get(sym, env) pd <- setNames((Hi(x) - Op(x)) / Op(x), "PD") merge(x, pd) } # ...
  • 我没有看源代码来理解为什么会这样,但它似乎做你想要的。 基本上,您通过TA参数将addLines调用添加到chartSeries 。 这通常是一件好事,因为它避免了为每个add*调用重新绘制图表。 chartSeries(cache$AAPL, subset="last 3 months", TA="addVo();addLines(v=10,on=-1)") I didn't look at the source code to understand why this works, but it see ...
  • 您需要将引用数据从data.frame转换为xts对象,并为调整后的价格添加一列。 然后你就可以了。 getSymbols("SPY", src='yahoo', to='2012-10-31') spy.quote = getQuote("SPY", what = yahooQuote.EOD) # convert to xts xts.quote <- xts(spy.quote[, -1], as.Date(spy.quote[, 1])) # use Date for indexClass xts ...
  • 你可以做那样的事情。 使用样式1从money转换为varchar会增加千位分隔符: declare @value int = 4255 select '$ ' + replace(convert(varchar(100), convert(money, @value), 1), '.00', '') -- output: $ 4,255 如果您在任何应用程序中处理来自SQL Server的数据,则在代码中格式化更容易。 You can do something like that. Converting ...
  • 尝试 library(quantmod) values <- oanda.currencies #> head(values) # oanda.df.1.length.oanda.df...2....1. #USD US Dollar #AFN Afghanistan Afghani #ALL Albanian Lek #DZD ...
  • Rpy2的importr()试图转换任何“。” 在R对象名称为“_”用于Python。 但是,只要有两个R对象名称带有“。”。 或“_”(两个字符对R中的名称有效)rpy2报告错误。 这里的R包“xts”定义了两个对象.subset_xts和.subset.xts 。 解决方法是手动指定如何转换名称: from rpy2.robjects.packages import import xts = importr("xts", robject_translations = {".subset.xts": "_ ...
  • 它不起作用,因为xts / zoo对象是一个带索引属性的矩阵,你不能在矩阵中混合类型。 GSPC包含数字数据,因此您只能添加数字列。 It doesn't work because xts/zoo objects are a matrix with an index attribute and you can't mix types in a matrix. GSPC contains numeric data, so you can only add numeric columns.
  • 正如“ ?stockSymbols所说: stockSymbols返回的符号可能不是使用getYahooData检索数据所需的格式。 请注意, TTR::getYahooData从Yahoo Finance下载数据,就像getSymbols.yahoo一样。 在使用使用随机数生成器(即sample )的函数时,您还应该使用set.seed ,这样您的示例将是可重现的。 还有其他一些要记住的事情: 雅虎财经,谷歌财经等不一定提供所有工具的历史数据。 股票返回的工具stockSymbols不一定是股票。 它们可 ...
  • 请注意,您不能在R中的for循环内更改迭代器。因此,for循环结束时的x = x + 1是不必要的,并被忽略。 另请注意,for循环中禁用了自动打印,因此您需要显式调用print 。 您可以做的另一件事是直接迭代向量(不需要子集)。 所以你的for循环可能如下所示: for(y in year) { print(viewFin(GE.f, type="IS", period="A")["Net Income", y]) } 也就是说,for循环是不必要的。 您可以直接使用子集获得相同的结果。 net ...
  • 您错过了(重要的)事实,即bdh()仍然返回您需要先变换的data.frame对象: R> library(Rblpapi) Rblpapi version 0.3.5 using Blpapi headers 3.8.8.1 and run-time 3.8.8.1. Please respect the Bloomberg licensing agreement and terms of service. R> spy <- bdh("SPY US EQUITY", c("Open","High","L ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)