gpt4 book ai didi

java - 回去 Activity

转载 作者:行者123 更新时间:2023-11-29 08:07:28 25 4
gpt4 key购买 nike

我可以从我的 mainActivity 转到我的 SoundActivity 类,这是一个菜单。但是,当我单击设备后退按钮时,它不会返回到我的 mainActivity 而只是关闭应用程序。我已将 finish() 放在末尾,但这似乎没有用。我也试过 super.backPressed() 但也没有用。这是声音类的代码

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.ToggleButton;

public class SoundActivity extends Activity
{
MediaPlayer ourSong;

private SeekBar volumeSeekbar = null;
private ToggleButton muteButton = null;
private AudioManager audioManager = null;

private AudioManager mAm;
private boolean mIsMute;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.soundmenu);
initControls();


ourSong = MediaPlayer.create(SoundActivity.this, R.raw.beat2);
ourSong.start();

}



private void initControls()
{
try
{
volumeSeekbar = (SeekBar)findViewById(R.id.sbVolumeBar);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));



volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onStopTrackingTouch(SeekBar arg0)
{
}

public void onStartTrackingTouch(SeekBar arg0)
{
}

public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});

muteButton = (ToggleButton)findViewById(R.id.toggleButton1);
muteButton.setOnClickListener(new View.OnClickListener()
{

public void onClick(View v) {
// TODO Auto-generated method stub
if(muteButton.isChecked())
{
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
}
else
{
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}



@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();

}
}

如果有人能帮助我,我将不胜感激。一时糊涂

ok 这是启动声音 Activity 的 mainActivity

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;
import android.os.Bundle;

public class PinballShooterActivity extends Activity {
/** Called when the activity is first created. */
MediaPlayer ourSong;

private ToggleButton muteButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);



ourSong = MediaPlayer.create(PinballShooterActivity.this, R.raw.beat2);
ourSong.start();

Button soundBtn = (Button) findViewById(R.id.sound);
soundBtn.setOnClickListener(new View.OnClickListener() {



public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(PinballShooterActivity.this, SoundActivity.class));
}
});

Button highScrBtn = (Button) findViewById(R.id.highscores);
highScrBtn.setOnClickListener(new View.OnClickListener() {



public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(PinballShooterActivity.this, HighScoreActivity.class));
}
});

Button gameBtn = (Button) findViewById(R.id.startgame);
gameBtn.setOnClickListener(new View.OnClickListener() {


public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(PinballShooterActivity.this, GameActivity.class));
}
});
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}

这是安卓 list

    <uses-sdk android:minSdkVersion="10" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SoundActivity" />
<activity
android:name=".HighScoreActivity" />
<activity
android:name=".GameActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.pinball.shooter.GAMEACTIVITY" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PinballShooterActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.pinball.shooter.faiz.PINBALLSHOOTERACTIVITY"/>

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

最佳答案

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}

您的 onPause() 中有 finish()。每次调用 SoundActivity 时,MainActivity 都会进入后台并调用其 onPause(),后者又会调用您的 finish() 并结束 Activity。

关于java - 回去 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10182123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com