-1

I have an Activity from where I have started a Service. The service displays a layout in landscape mode. My problem is if the service displays anything in landscape mode the activity also goes to landscape mode and if the service layout goes to portrait mode the activity layout too goes to portrait mode. I just want to stop the activity from going to landscape mode while service is doing some stuff on landscape mode. I just want to retain the activity in portrait mode. I have tried through manifest but no luck. Is there any solution?

Zakir
  • 2,222
  • 21
  • 31
  • "The service displays a layout in landscape mode" -- you may wish to explain in detail what you mean by this, such as via a [mcve]. – CommonsWare Jun 18 '17 at 19:17

3 Answers3

0

Services do not have any layout or UI. Read up basics of a service from Android doc

A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface. Another application component can start a service, and it continues to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

You probably have 2 Activities and to control orientation there are tons of SO threads already like:- Android - disable landscape mode?
How do I disable orientation change on Android?

Zakir
  • 2,222
  • 21
  • 31
  • yes you are right a service has no ui.but i am using window manager to display a view inflated from an xml.so rounding up, i have an activity and an service.i want to fix the orientation of activity to portrait but whn the service goes to landscape mode the activity view also goes to landscape mode.tried through th manifest using but no luck – Nizam Ahmed Jun 18 '17 at 19:30
  • the moment the service sets the view to landscape the activity also goes to landscape.as soon as the service closes it comes back to portrait.the issue is why the activity goes to land. – Nizam Ahmed Jun 18 '17 at 19:52
0

A service performs in background long running operations. It has not user interface.

dboem
  • 3
  • 3
0

You don't need to worry about service.

If you want to keep view permanent for particular activity then

In the manifest, set this for your activity where start service

<activity android:name=".YourActivity"
android:configChanges="orientation"
android:screenOrientation="landscape"/> // or portrait

or for conditional situation (temporary) you can do

if(isServiceStarted){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

}
if(isServiceEnded){
//flip to original view
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

I hope it will work for you.

Updated Ans

What happen when orientation changed Life Cycle of orientation

onPause();
onSaveInstanceState();
onStop();
onDestroy();

onCreate();
onStart();
onResume();

make 2 methods

void changeToLandscape(){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
void changeToPro(){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

now when you want to rotate view

onCreate(){
//  start service here and rotate view
  changeToLandscape();
}

then it will go in onResume()

void onResume(){
   super();
   if(isServiceOn){
      changeToLandscape()
   }
   else{
     changeToPro()
   }
}
void onPause(){
       super();
       if(isServiceOn){
          changeToLandscape()
       }
       else{
         changeToPro()
       }
    }
Mahesh Lad
  • 1,997
  • 1
  • 9
  • 8
  • Handler h; Runnable r; public void orien(){ h = new Handler(); r = new Runnable() { @Override public void run() { if(fullscreen){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); h.postDelayed(r, 1); }else{ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); h.postDelayed(r, 1); } } };h.post(r); } – Nizam Ahmed Jun 18 '17 at 19:47
  • tried with this handler but still not working.thanks for ur reply – Nizam Ahmed Jun 18 '17 at 19:48
  • the moment the service sets the view to landscape the activity also goes to landscape.as soon as the service closes it comes back to portrait.the issue is why the activity goes to land. – Nizam Ahmed Jun 18 '17 at 19:53
  • try code in onResume() method bcoz on change of view activity and service restarts – Mahesh Lad Jun 18 '17 at 19:57
  • Dint understand.could u please tell in a elaborate way – Nizam Ahmed Jun 18 '17 at 19:59
  • What happen when orientation changed Life Cycle of orientation 'onPause(); onSaveInstanceState(); onStop(); onDestroy(); onCreate(); onStart(); onResume();' – Mahesh Lad Jun 18 '17 at 20:03
  • Actually i have tried it in a different way.Irrespective of the orientation i have rotated the view and a handler is used to lock the rotation of the activity beforehand.Worked well.But its seems its not the way it should be but did the job. – Nizam Ahmed Jun 21 '17 at 06:11