简体   繁体   English

仅在第二次访问活动时才发生OnResume

[英]OnResume to only happen when activity is accessed the second time

I am trying to write code for my android application for things to happen when the activity is Resumed. 我正在尝试为我的android应用程序编写代码,以恢复活动恢复时发生的事情。 Although I only want this stuff to happen when the activity is re-visited for the second time and not the first time. 尽管我只希望第二次而不是第一次重新访问该活动时发生这种事情。

So I want the user to do what they need in the onCreate method in their first visit to the activity and then when they go back to the activity then the onResume code happens. 因此,我希望用户在第一次访问活动时执行onCreate方法中需要做的事情,然后当他们返回活动时,就会发生onResume代码。

Should I be using a different method or how can I achieve what I am looking to do? 我应该使用其他方法还是要实现自己的目标?

public void onResume(){
    super.onResume();

    RunningStatus = sharedPreferences.getBoolean("RunningStatus", false);

    if (isS1Pressed) {
        if (isPressed) {
            if (RunningStatus = false) {
                pause.setBackgroundResource(R.drawable.start);
            }
        }
    }

EDIT: 编辑:

I don't mean second time I mean every time after. 我的意思不是第二次,而是以后的每次。 Basically I have a production line and my app determines whether it is ahead or behind. 基本上我有一条生产线,我的应用程序确定它是领先还是落后。 So when the user enters this activity then the can perform the calculation. 因此,当用户输入此活动时,则可以执行计算。 We have a pause button where the user can pause the line if the production line has been stopped in real life. 我们有一个暂停按钮,如果生产线实际上已经停止,用户可以在其中暂停生产线。 Although I would like that the user can then go out of that specific production line and do calculations on other lines too. 尽管我希望用户随后可以退出该特定生产线,并在其他生产线上进行计算。 Although at the minute once I leave the activity I lose all its state. 尽管在离开活动的那一刻我失去了所有状态。

Thanks 谢谢

You could use a boolean flag. 您可以使用布尔标志。 in onResume eg onResume例如

if (secondVisit) {
  ...
} else secondVisit = true;

If you want to do some stuff for very specific time (only 2nd, not 1st or 3rd, 4th,...), I suggest you should use some counter variable and use activity method onSaveInstanceState() and onRestoreInstanceState() to store and retrieve. 如果您想在非常特定的时间内做一些事情(仅第二个,而不是第一个或第三个,第四个……),我建议您使用一些计数器变量并使用活动方法onSaveInstanceState()和onRestoreInstanceState()进行存储和检索。 If you don't want to reset the counter when app got killed, use data persist like SharePreference to store counter. 如果您不想在应用程序被杀死时重置计数器,请使用诸如SharePreference之类的数据持久存储计数器。

i assume that you have gone through the activity life cycle if not then visit https://developer.android.com/guide/components/activities/activity-lifecycle 我假设您已经经历了活动生命周期,那么请访问https://developer.android.com/guide/components/activities/activity-lifecycle

boolean secondUse=false;//this should be a field in the activity class

onRestart() method is called only when we visited the activity and and came back and then we visit(ie when the activity already in the activity stack). onRestart()方法仅在我们访问活动然后又返回然后访问时才调用(即,当活动已经在活动堆栈中时)。

if you have other use case, then using sharedPreference would be better. 如果您还有其他用例,那么使用sharedPreference会更好。

override the onRestart() inside it write the following 覆盖其中的onRestart()编写以下内容

if(!seconduse){
//do your stuff
secondUse=true;
}

as you said in the edit you need the stuff to work on every visit after first visit then simply put your code in onRestart() method 就像您在编辑中所说的那样,您需要在第一次访问后的每次访问中都需要处理这些东西,然后将您的代码放入onRestart()方法中

public void onRestart(){
super.onRestart();

RunningStatus = sharedPreferences.getBoolean("RunningStatus", false);
   if (isS1Pressed) {
    if (isPressed) {
        if (RunningStatus = false) {
            pause.setBackgroundResource(R.drawable.start);
        }
    }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当恢复活动时才onResume() - onResume() only if Resume Activity 第二次访问时,会话变量为空 - Session variable is null when accessed second time Tomcat在任何浏览器上第二次访问时都提供未找到的页面 - Tomcat gives page not found when accessed for second time on any Browser 如何仅在活动进入屏幕时调用“ onResume”,而不是通过其他方法调用它 - How to make `onResume` only be called when the activity comes to screen and not by other methods calling it 返回活动时未调用Android onResume() - Android onResume() not being called when returning to an Activity 从片段返回时,不会调用主页活动的 OnResume() - OnResume() is not called of the home activity when back pressed from a fragment 片段未附加到活动,位于onResume中 - Fragment not attached to Activity, in onResume Java - 在访问时将数据加载到Singleton中,但仅在经过一段时间后才加载 - Java - load data into a Singleton when it is accessed, but only after time has passed Activity 加载时第一次获得 NullPointerException 但第二次工作正常 - Getting NullPointerException first time when Activity loads but working fine second time Mockito 仅在第二次调用方法时返回值 - Mockito return value only when method is called for second time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM