英文:
Add a back button in activity
问题
以下是您要翻译的内容:
代码部分不要翻译, 只返回翻译好的部分, 不要有别的内容, 不要回答我要翻译的问题。
早上好,大家好!我有一个代码,在活动开始时播放音频,进行欢迎问候。然而,当我转到其他活动并返回主菜单时,音频会再次播放。我希望当我使用返回操作栏按钮时,能解决这个问题,但我需要在片段或活动中使用一个按钮,因为我的应用中不能有操作栏。
播放音频的代码:
new Timer().schedule(new TimerTask() {
@Override
public void run() {
MediaPlayer play = MediaPlayer.create(MainActivity.this, R.raw.audioboatarde);
play.start();
}
}, 1000);
片段中的按钮代码:
button = view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1 = new Intent(getContext(), MainActivity.class);
startActivity(intent1);
}
});
英文:
Good morning, Guys I have a code that plays an audio when the activity starts it performs a welcome greeting, however when I go to other activities and return to the main menu the audio is played again I would like to notice that when I use the back action bar button I don't have this problem, but I need to use a button in the fragment or activity, I can't have an action bar in my app
Code to Play audio:
new Timer().schedule(new TimerTask() {
@Override
public void run() {
MediaPlayer play= MediaPlayer.create(MainActivity.this,R.raw.audioboatarde);
play.start();
}
}, 1000);
Code button on fragment:
button = view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1 = new Intent(getContext(), MainActivity.class);
startActivity(intent1);
}
});
答案1
得分: 2
停止在活动中的媒体播放,需在活动的onPause
方法中执行:
@Override
public void onPause() {
super.onPause();
play.stop();
}
而在第二个活动中,点击按钮后无需重新启动前一个活动,只需调用onBackPressed()
方法返回前一个活动:
button = view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
英文:
Stop your media playback when you leave the activity in onPause
of the Activity
@Override
public void onPause() {
super.onPause();
play.stop();
}
And you don't need to restart the previous activity on click of the button in the second activity, you just need to call onBackPressed()
to go to the previous activity.
button = view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
答案2
得分: 0
这是我的第二个回答。
在情况下,如果您必须从第二个活动重新启动第一个活动(如果它被销毁),您还可以使用onSaveInstanceState(Bundle outState)
在活动中保存播放的音频状态。
在第一个活动中:
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean("isPlayed", true);
super.onSaveInstanceState(outState);
}
在第一个活动的onCreate()
方法中:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null && savedInstanceState.getBoolean("isPlayed") == false) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
MediaPlayer play = MediaPlayer.create(MainActivity.this, R.raw.audioboatarde);
play.start();
}
}, 1000);
}
}
稍后,您可以从第二个活动重新启动第一个活动。
button = view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1 = new Intent(getContext(), MainActivity.class);
startActivity(intent1);
}
});
不要忘记在第一个活动中停止音频播放。
@Override
public void onPause() {
super.onPause();
play.stop();
}
英文:
This is my second answer.
In the case you have to relauch the first activity(if it is destroyed) from the second one,
you can also use onSaveInstanceState(Bundle outState)
to save the audio played state in the activity.
In the first activity
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean("isPlayed",true);
super.onSaveInstanceState(outState);
}
In onCreate()
of the first activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null && savedInstaceState.getBoolean("isPlayed") == false) {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
MediaPlayer play= MediaPlayer.create(MainActivity.this,R.raw.audioboatarde);
play.start();
}
}, 1000);
}
}
Later on, you can relaunch the first activity from the second activity.
button = view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1 = new Intent(getContext(), MainActivity.class);
startActivity(intent1);
}
});
Dont forget to stop the audio playback in the first activity.
@Override
public void onPause() {
super.onPause();
play.stop();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论