简体   繁体   English

在活动的 onPause() 或 onStop() 状态运行代码

[英]Running code in onPause() or onStop() state of the activity

There is a function in my android app that needs to run every time the user tries to edit his or her profile.我的 android 应用中有一个功能需要在用户每次尝试编辑他或她的个人资料时运行。 There are two parts of edit profile in my app (please don't ask why, it has a very long tedious reason behind it).我的应用程序中有两个部分的编辑配置文件(请不要问为什么,背后有一个很长很乏味的原因)。 I need to revert back the changes the user did in the first part of the edit profile if the user decides to cancel everything.如果用户决定取消所有内容,我需要恢复用户在编辑配置文件的第一部分所做的更改。 I have made a cancel button in the part two of edit profile but my question is, what if user presses the return button or the home button on the device and the app calls the onPause() and on onStop() ?我在编辑配置文件的第二部分做了一个取消按钮,但我的问题是,如果用户按下设备上的返回按钮或主页按钮并且应用程序调用onPause()onStop()怎么办? how can I run the same code in these two phases of the activities?如何在活动的这两个阶段运行相同的代码? Anyone out there who knows how to put code in different states on activities?有谁知道如何将代码置于不同状态的活动? Do I just make a function onPause() and stick the code in there?我只是创建一个函数onPause()并将代码粘贴在那里吗? Would that work?那行得通吗?

Yes, it should definitely work.是的,它绝对应该有效。 In your case, you should write your code in onPause() method.在您的情况下,您应该在 onPause() 方法中编写代码。

Here is a summary of the Activity Lifecycle:以下是活动生命周期的摘要:

onCreate(): onCreate():

Called when the activity is first created.在第一次创建活动时调用。 This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.这是您应该进行所有常规静态设置的地方:创建视图、将数据绑定到列表等。此方法还为您提供了一个包含活动先前冻结状态的 Bundle(如果有的话)。 Always followed by onStart().总是跟在 onStart() 之后。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Write your code here
}

onStart():开始():

Called when the activity is becoming visible to the user.当活动对用户可见时调用。 Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.如果活动进入前台,则后跟 onResume(),如果活动变为隐藏,则后跟 onStop()。

@Override
public void onStart() {
    super.onStart();
    //Write your code here
}

onResume(): onResume():

Called when the activity will start interacting with the user.当活动开始与用户交互时调用。 At this point your activity is at the top of the activity stack, with user input going to it.此时,您的活动位于活动堆栈的顶部,用户输入进入它。 Always followed by onPause().后面总是跟着 onPause()。

@Override
public void onResume() {
    super.onResume();
    //Write your code here
}

onPause():暂停():

Called when the system is about to start resuming a previous activity.当系统即将开始恢复之前的活动时调用。 This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.这通常用于提交对持久数据的未保存更改、停止动画和其他可能消耗 CPU 的内容等。此方法的实现必须非常快,因为在此方法返回之前不会恢复下一个活动。 Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.后跟 onResume() 如果活动返回到前面,或者 onStop() 如果它对用户不可见。

@Override
public void onPause() {
    super.onPause();
    //Write your code here
}

onStop():停止():

Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one.当 Activity 不再对用户可见时调用,因为另一个 Activity 已经恢复并且正在覆盖这个 Activity。 This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.这可能是因为一项新活动正在开始,一个现有活动被带到这个活动之前,或者这个活动正在被破坏。 Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.如果此 Activity 回来与用户交互,则后跟 onRestart(),如果此 Activity 正在消失,则后跟 onDestroy()。

@Override
public void onStop() {
    super.onStop();
    //Write your code here
}

onDestroy():销毁():

The final call you receive before your activity is destroyed.在您的活动被销毁之前您收到的最后一个电话。 This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.这可能是因为活动正在完成(有人在其上调用了 finish() ,或者因为系统暂时销毁了该活动的实例以节省空间)。您可以使用 isFinishing() 方法区分这两种情况。

@Override
public void onDestroy() {
    super.onDestroy();
    //Write your code here
}

You can do many things inside both onPause and onStop, just remember to call super.onPause();你可以在 onPause 和 onStop 里面做很多事情,只要记住调用super.onPause(); , super.onStop(); , super.onStop(); or whatever you need inside each one, just follow the pattern below.或者任何你需要的东西,只需按照下面的模式。 Simply add the code to your Activity and you're good to go.只需将代码添加到您的活动中即可。

@Override
public void onPause() {
    super.onPause();  // Always call the superclass method first
    // Do what you want.
}

Additionaly, if you want your users to be able to go back on your activity and edit something instead of closing it, you can just call onBackPressed() :此外,如果您希望您的用户能够返回您的活动并编辑某些内容而不是关闭它,您只需调用onBackPressed()

@Override
public void onBackPressed() {
     super.onBackPressed();
     // You can just call onStop to close the app
     // or do what you want.
}

只有 onPause 保证被调用

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM