在活动中添加一个返回按钮。

huangapple go评论86阅读模式
英文:

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();
    }

huangapple
  • 本文由 发表于 2020年5月29日 20:01:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/62085511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定