0

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

}

} );

}

}
Matt Wolfe
  • 8,924
  • 8
  • 60
  • 77
Crash
  • 21
  • 1
  • 3

6 Answers6

2

I added to the manifest: android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"

and to the Activity Java file.

@Override public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);     
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    } 

The layout stays in Landscape and rotation of the orientation does not affect the playing of the app.

Crash
  • 21
  • 1
  • 3
1

This happens because your activity is destroyed and restarted during orientation change (also called a Configuration Change in Android-land).

In your AndroidManifest.xml file, add the following to your activity declaration:

android:configChanges="orientation"

So that it looks something like the following:

        <activity android:name="com.your.package.TestAppActivity"
        android:configChanges="orientation"
    />

Your activity can then listen to configuration changes and respond manually. Changing orientation will no longer automatically restart the activity.

John O'Connor
  • 5,244
  • 2
  • 24
  • 29
1

I still didn't get the result I was looking for, the content still changed when I rotated the device (the onCreate() method was still called). The actual effect I achieved when I gave an empty configuration Object

new Configuration() instead of the provided parameter newConfig

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(new Configuration());
}

Hope this helps whoever wanted a total ignore of orientation change events

VeRo
  • 956
  • 13
  • 27
0

This is the way Android Activity has been designed. It will reload the new activity if the orientation changes. Either you disable the orientation or divide your application in 2 tiers (front end and back end).Front end will just display the progress and control buttons and back end will take the command from front end and execute. In this model you will not have the issue which you are facing now.

Take a look of this link it might help you.

Community
  • 1
  • 1
Rakesh
  • 3,987
  • 10
  • 43
  • 68
0

Take a look at this article: Handling Runtime Changes

I think it's better handle the change, but you still have the option to preventing it, as described in "b. Handle the configuration change yourself".

Chopin
  • 1,442
  • 17
  • 24
0

In the manifest, in your tag, put android:screenOrientation="portrait"

http://developer.android.com/guide/topics/manifest/activity-element.html

Yusuf X
  • 14,513
  • 5
  • 35
  • 47