如何实现onRetainNonConfigurationInstance [英] how to implement onRetainNonConfigurationInstance

查看:384
本文介绍了如何实现onRetainNonConfigurationInstance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做Android的媒体播放器
它的工作很好,但是当我改变屏幕的方向,该活动正在重新启动
我知道,这个问题已经问计算器上几次
但没有答案的帮助了我。
我想我应该使用:onRetainNonConfigurationInstance

i made a media player in android it's working great but when i change the screen orientation, the activity is being restarted i know that this question was already asked on stackoverflow several times but none of the answers helped me. i think i should use: onRetainNonConfigurationInstance

    @Override 
public Object onRetainNonConfigurationInstance() { ... }

但我不知道来实现它的正确方法
所以,如果有人能够给我一个教程或隐式的例子,我将不胜感激。

but i didn't know the correct way to implement it so if someone could give me a tutorial or an implicit example i would be grateful

推荐答案

没关系,它是pctated德$ P $,它工作正常。最简单的是:

Never mind that it's deprectated, it works fine. Simplest would be:

public Object onRetainNonConfigurationInstance() {
   return this;
}

然后在 YourActivity 的onCreate()

public void onCreate(Bundle savedState)
{
   YourActivity prevActivity = (YourActivity)getLastNonConfigurationInstance();
   if(prevActivity!= null) { 
       // So the orientation did change
       // Restore some field for example
       this.myValue = prevActivity.myValue;
   }
}

这篇关于如何实现onRetainNonConfigurationInstance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆