开发者

the activity restarts when the phones View Mode is changed

开发者 https://www.devze.com 2023-04-13 00:25 出处:网络
In my application while playing a video if i change the phone\'s mode the video does not continue to play but it starts from the beginning. all i understand is th开发者_开发问答e activity is recreated

In my application while playing a video if i change the phone's mode the video does not continue to play but it starts from the beginning. all i understand is th开发者_开发问答e activity is recreated when the mode is changed,how do i fix this i have no idea please someone help me fix this. thanks in advance


You can add this line in your AndroidManifest file to your activity tag, by this your activity won't get re-started.

<activity android:name=".Activity_name"
          android:configChanges="orientation|keyboardHidden">


when your activity restarts save the current postion of the video thru this method:

@Override
public Object onRetainNonConfigurationInstance() {
    int videoPosition = videoView.getCurrentPosition();
    Bundle data = new Bundle();
    data.putInt("POSITION", videoPosition);
    return data;
}

and on onCreate() method retrieve this value like this:

final Bundle data = (Bundle) getLastNonConfigurationInstance();

// The activity is starting for the first time...
if (data == null) {
   // start your video for the first time here..
} else {
    // Resume your video from where it was left..
    int videoPosition = data.getInt("POSITION");
// start your video from videoposition....          
}
0

精彩评论

暂无评论...
验证码 换一张
取 消