1

I am having a problem in screen orienation in my application. I created an alternate layout in res/layout-lan folder for landscape mode. The problem occurred during orientation change,

1.An activity get recreated without destroying the old activity.

2.since i am using mediaplayer in my app,on screen rotation the .mp3 is playing on both orientation concurrently..

I am unable to find proper sample code for handling screen orientation with saving and restoring the state of the activity..............

user632475
  • 31
  • 1
  • 5

4 Answers4

3

1.An activity get recreated without destroying the old activity.

The recreation of the Activity is the natural default behavior of Android when a configuration change occurs. The likely reason your old Activity lingers in memory is because it is referencing a currently playing instance of a MediaPlayer.

Because you are using different layout resources for landscape and portrait, it is to your advantage to let Android recreate the Activity and pull the appropriate resources each time. If you handle rotation yourself, you will be responsible to reloading the proper layout as well.

2.since i am using mediaplayer in my app,on screen rotation the .mp3 is playing on both orientation concurrently..

There are two solutions to this issue...

The ideal solution is to move your media playback into a Service. The Activity can call the Service to start/stop/etc. playback when directed by the user, but putting this into a background component like a Service allows it to operate continuously even when your Activity is in flux due to the changes. This is the design pattern the Android team encourages, where your Activity really only deals with user interface.

Another workable solution is to pass your MediaPlayer from the old Activity to the new one using onRetainNonConfigurationInstance(). This allows the single MediaPlayer to exist between Activity instances, keeping the playback consistent. For example:

public class MyActivity extends Activity {
    private MediaPlayer mPlayer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Check if we have a player passed in from the last instance
        mPlayer = (MediaPlayer)getLastNonConfigurationInstance();
        //If not, make a new one
        if (mPlayer == null) {
            mPlayer = new MediaPlayer();
           //...Set up new player instance...
        }
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        //Clear our member variable to guarantee this Activity
        // is allowed to GC after onDestroy()
        MediaPlayer instance = mPlayer;
        mPlayer = null;
        //Hand our current player up to the next Activity to be created
        return instance;
    }

}

Another option to ensure the best memory cleanup would be to define mPlayer as a WeakReference<MediaPlayer> to allow the GC to claim the old Activity, even if the MediaPlayer is playing audio at the time of the configuration change.

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Happy to help. If this answer was useful to you, please feel free to accept or upvote it. Cheers! – devunwired Jul 12 '12 at 14:54
  • In my application having two instances of Mediaplayer like mplayer and mplayer1 .can i be able to retain both the instance simulatenously..... – user632475 Jul 14 '12 at 15:57
  • 1
    You can only retain one object, but that object could be a collection (i.e. `List` or `Map`) so you could pack your instances into a collection object and retain that instance instead. – devunwired Jul 14 '12 at 16:55
0

you can stop recreation of activity when Screen orientation changed by below steps.

set configChanges tag as below

 <activity android:name=".Activity_name"
          android:configChanges="orientation|keyboardHidden">

use below method.

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
   // to do on orientation changed          
    }
    }
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • 1
    OP Beware that if this approach is used, you must call `setContentView()` again manually in order for the alternative layout to be picked up and drawn. By default, it will just rotate the `Activity` and keep the same layout in place. – devunwired Jul 11 '12 at 18:32
  • i am placing the landscape layout in layout-land folder.......can you please tell me how to avoid recreation of activity by this method ... – user632475 Jul 12 '12 at 12:56
  • in onCOnfichange you can check which orientation it is, and do needful – AAnkit Jul 12 '12 at 13:23
0
                       v 

Create Folder res/layout-land

Instead of res/layout-lan <---- put here d in folder-name

Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33
0
//by changing the orientation if your layout not disturb in design just data 

//destroyed //then use the below attribute in manifest in activity you want to do

  android:configChanges="orientation|screenSize|layoutDirection"

//but your whole screen design disturb then turn off orientation by using this

 android:screenOrientation="portrait"
Mazhar Iqbal
  • 813
  • 7
  • 7