我正在编写一个简单的应用程序,其中活动A播放了一首歌。按下按钮时,媒体播放器暂停,活动B打开。当用户返回活动A时,媒体播放器应该恢复(从它停止的地方)。我的问题是,当您按下活动B上的后退按钮会发生什么?我知道活动B会被杀死,但是来自活动A的onResume被加载了?我在哪里提供恢复指示?
答案 0 :(得分:0)
onResume method will be called when a activity is resumed.
在任何类型的Android编程之前,你似乎必须阅读Activity Lifecycle
答案 1 :(得分:0)
我的代码如下
public class Homework extends Activity {
final MediaPlayer mediaplayer = new MediaPlayer();
int media_length;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homework);
setContentView(R.layout.activity_homework);
AssetFileDescriptor afd;
try {
afd = getAssets().openFd("rev.mp3");
mediaplayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mediaplayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaplayer.start();
Button next = (Button) findViewById(R.id.homeworkContinue);
final Intent openCloser = new Intent(this, EndActivity.class);
next.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
mediaplayer.pause();
media_length = mediaplayer.getCurrentPosition();
startActivity(openCloser);
}
});
}
public void onResume(Bundle savedInstanceState){
super.onResume();
mediaplayer.seekTo(media_length);
mediaplayer.start();
}
}