在片段保存在Android的屏幕旋转数据 [英] Save data in Fragments on screen rotation in Android

查看:149
本文介绍了在片段保存在Android的屏幕旋转数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我解决我的问题?

Can anyone help me to solve my problem?

我有一个活动和六个片段。

I have an activity and six fragments.

一个片段包含一个链接到我的MediaPlayer类,我用它来显示一个片段一些视频。结果
我得到一个链接到一个单独的onCreate()我活动的方法每个片段类。结果
每个屏幕旋转时,onCreate()方法被调用,我得到新的链接,参加六片段类(和新的MediaPlayer)。

One fragment contains a link to my MediaPlayer class, which I use to show some video in a fragment.
I get a link for every Fragment class in onCreate() method of my Activity.
Each time the screen rotates, the onCreate() method is called and I get new links to my six Fragment classes (and new MediaPlayer).

我可以保存每一个环节,以我的片段类旋转前,然后拿回来?结果
(例如:我保存指向我的片段与链接到媒体播放器类,然后调用我的MediaPlayer的stop()方法,那么当屏幕旋转时,我恢复所有数据,以及我的MediaPlayer,并调用ResumePlay()方法) 。

Can I save every link to my Fragment classes before rotation and then get it back?
(For example: I am saving links to my fragments with the link to media player class, then call stop() method of my MediaPlayer, then when the screen rotated, I restore all data, and my MediaPlayer and call ResumePlay() method).

或者,这是一个坏主意,我需要重新连接每一次,我需要做的唯一的事情就是保存录像和路径的当前时间的onSaveInstanceState到文件,然后拿回来在onRestoreInstanceState,开始新的MediaPlyer随着时间的推移被恢复?

Or is this a bad idea and I need recreate links every time, and the only thing I need to do is to save the current time of video and path to file in onSaveInstanceState, then get it back in onRestoreInstanceState, start new MediaPlyer with time that was restored?

感谢您!

推荐答案

用于扩展您的应用程序商店里面的片段你想,当你改变的onSaveInstanceState方法中屏幕的方向,然后保留onCreateView方法内保留它的价值每个类。

for each class extending fragment inside your application store the value you want to retain it when you change the screen orientation inside onSaveInstanceState method and then retain inside onCreateView method.

例如对于您遇到的视频问题,你可以做这样的事情:

for example for the video problem you are having , you can do something like this:

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("cp", videoview.getCurrentPosition());  
 }

再保留它像这样

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if(savedInstanceState!=null)
    {
        int a=savedInstanceState.getInt("cp");
        videoview.seekTo(a);
    }

//the rest of your code then....
}

希望我给你。

这篇关于在片段保存在Android的屏幕旋转数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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