I want to know how to keep the app from reloading when the orientation is changed?
I have a small test app that will play an audio sound until the stop button is pressed. If I change my Android or the simulator from horizontal to portrait then the stop button does not work. A second occurrence of the app is loaded. I want the first occurrence to keep running regardless if the orientation has been changed.
Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.64" android:orientation="horizontal">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
</LinearLayout>
</LinearLayout>
Test.java
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class TestAppActivity extends Activity {
private Button btnDisplay;
private MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer = MediaPlayer.create(this, R.raw.nut);
mediaPlayer.setLooping(true);
((Button)findViewById(R.id.button1)).setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer = MediaPlayer.create(LoopBugActivity.this, R.raw.nut);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
} );
((Button)findViewById(R.id.button2)).setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer.stop();
}
} );
}
}