简体   繁体   English

在Android中完成活动后该怎么做?

[英]how to do something when an activity finished in android?

In one of my activity named ActivityGame there is a number( int score ) that is changing while the program is running, and I want to save the last value of this number. 在我的一个activity名为ActivityGame有一个数字( int score )的程序运行时,这种情况正在改变,我要保存此号码的最后价值。 In fact I want that value right before the activity closed. 实际上,我想在activity结束之前获得该值。 Is there any way to do this? 有什么办法吗?

Are you looking for something like this? 您是否正在寻找这样的东西? http://developer.android.com/training/basics/intents/result.html http://developer.android.com/training/basics/intents/result.html

onPause() is a premature release. onPause()是一个过早的发行版。 You can return the result with the onStop() method so that every time the user leaves the activity you can be sure you have the right value stored. 您可以使用onStop()方法返回结果,以便每次用户离开活动时都可以确保您存储了正确的值。 Alternatively use the onDestroy() method which ideally leads to a process kill. 或者使用onDestroy()方法,理想情况下会导致进程终止。

It all depends on how and what the data is used for. 这完全取决于数据的使用方式和用途。

You can use shared prefernce for this in onPause(): 您可以在onPause()中使用共享偏好设置:

void onPause(){

SharedPreferences sharedpreferences = getSharedPreferences("mypref", Context.MODE_PRIVATE);
        Editor editor=sharedpreferences.edit();
        editor.putString("key", scorevalue);
        editor.commit();
}

Gather all the values on OnPause() 收集OnPause()上的所有值

@Override
    protected void onPause() {
        super.onPause();
              // Gather the values from the entities here.
    }

and Save it on Stop() 并将其保存在Stop()上

@Override
protected void onStop() {
super.onStop();
//Save Values Here
}

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

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