When exiting app, change orientation from portrait to landscape - android

So I manually set the orientation in the onCreate to portrait in my app:
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
but I want to change (force) the layout back to landscape when I exit the application. Currently, if my device is locked in landscape and it changes to portrait mode when the app is started I want to be able to go back to landscape. And also vice versa, on onResume I want it to go back to portrait. Is there any way to do this?
EDIT: here is where I try to manually set the orientation back to landscape when I exit the application, but the orientation does not change. When I boot the kernel image its default is landscape orientation but when I change the orientation within an app, the orientation sets it globally on application menu and home screen.
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
myVideoView.pause();
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
position = savedInstanceState.getInt("Position");
myVideoView.seekTo(position);
}
#Override
public void onDestroy() {
super.onDestroy();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Log.d(CLASS_NAME, "-- ON DESTROY --");
}
#Override
public void onPause() {
super.onPause();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Log.d(CLASS_NAME, "-- ON PAUSE --");
}
#Override
public void onStop() {
super.onStop();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Log.d(CLASS_NAME, "-- ON STOP --");
}

Related

how to check if android going to recreate activity or destroy?

I have an android studio project. When I am rotating screen, android destroys and recreates main activity. How can I check during the destruction, if android going to recreate activity?
You can determine if the activity is finishing by user choice (user chooses to exit by pressing back for example) using isFinishing() in onDestroy.
#Override
protected void onDestroy() {
super.onDestroy();
if (isFinishing()) {
// wrap stuff up
} else {
//It's an orientation change.
}
}
Another alternative (if you're only targeting API>=11) is isChangingConfigurations.
#Override
protected void onDestroy() {
super.onDestroy();
if (isChangingConfigurations()) {
//It's an orientation change.
}
}
Override the Activity lifecycle methods to see the flow.And then use the appropriate method to check activity current state like isChangingConfigurations()
Example code snippet.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
Log.i(MainActivity.class.getSimpleName(),"OnStart Called");
}
#Override
protected void onRestart() {
super.onRestart();
Log.i(MainActivity.class.getSimpleName(),"OnRestart Called");
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.i(MainActivity.class.getSimpleName(),"OnDestroy Called");
}
#Override
protected void onPause() {
super.onPause();
Log.i(MainActivity.class.getSimpleName(),"OnPause Called");
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(MainActivity.class.getSimpleName(),"OnConfiguration Changed Called");
}
}
For more details see the official page activity-lifecycle

Handling rotation changes and user leaving activity

So I’ve got this Activity with a doSomething() method. This method must be called when the user leaves the Activity and resumes after a while. This code works fine. The problem is: When the user rotates the phone (orientation change), the method is also called. I don’t want the method to be called on Orientation Change. Here’s my Activity code
public class MainActivity extends AppCompatActivity
{
private static boolean callMethod=true;
#Override
protected void onResume() {
super.onResume();
if(callMethod)
doSomething();
}
#Override
protected void onPause() {
super.onPause();
callMethod=true;
}
private void doSomething()
{
Log.i(“doSomething()”,”Did something.”);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(callMethod)
doSomething();
}
}
Thanks in advance!
From API 13 you can use configChanges in manifest.
Add the following to the manifest. This prevents recreation of the activity on screen rotation:
<activity android:name=".Activity_name"
android:configChanges="orientation|keyboardHidden">
One note is after this, you should handle screen orientation change yourself. you should override the following function in your activity for that:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
setContentView(R.layout.layout_landscape);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.layout);
}
}
I see in your comment that you added the isChangingConfigurations() flag, which should do the trick, but you should persist that state rather than making it a static variable. Otherwise, if your process is killed when your app goes to the background you'll lose that state.
public class MainActivity extends AppCompatActivity {
private static final String KEY_CALL_METHOD = "key_call_method";
private boolean callMethod = true;
#Override
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.activity_main);
if (savedState != null) {
callMethod = savedState.getBoolean(KEY_CALL_METHOD);
}
}
#Override
protected void onResume() {
super.onResume();
if (callMethod) {
doSomething();
}
}
#Override
protected void onPause() {
super.onPause();
if (!isChangingConfigurations()) {
callMethod = true;
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(KEY_CALL_METHOD, callMethod);
}
private void doSomething() {
Log.i("doSomething()", "Did something.");
}
}

Android Black Screen after Unlocking

I'm having trouble with resuming an app's activity after locking the device screen.
Here's the typical situation:
1. App is running.
2. Lock device.
3. Unlock device.
4. Instead of returning to running app, it shows a black screen.
I checked how the activity lifecycle is running and it seems this is what's happening when it unlocks:
1. Create
2. Start
3. Resume
4. Pause
Why is it stuck on pause? I'm sure this is really simple but as I'm still learning Android I'm super confused. Thanks for any help given!
EDIT:
I don't think I'm doing anything out of the ordinary but here's the basic code I'm using...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Java", "Create");
// Initialize NDK audio properties here
// Initialize the Content View
setContentView(R.layout.activity_main);
// Setup toolbar
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(myToolbar);
// Start thread
thread = new Thread() {
public void run() {
setPriority(Thread.MAX_PRIORITY);
// Audio Thread is running here
}
};
thread.start();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
#Override
protected void onStart() {
super.onStart();
}
#Override
protected void onPause() {
super.onPause();
if (thread != null) {
try {
thread.join();
}
catch (InterruptedException e) {
// e.printStackTrace();
}
thread = null;
}
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onStop() {
super.onStop();
}
Try this (it will instruct your app to save state):
protected void onSaveInstanceState(Bundle icicle) {
icicle.putLong("param", 1);
super.onSaveInstanceState(icicle);
}
I am not thread expert. but here it seems you are blocking ui thread in onpause. thread.join causes the current thread(in this case ui thread) to pause execution until thread terminates.

How to keep video inside fragment playing after rotation?

I managed to get my video to play with VideoView but after I rotate my device video stops and starts playing from the beginning. I placed my VideoView inside fragment.
I tried this inside my fragment but without success:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onPause() {
super.onPause();
if(video.isPlaying()) {
video.pause();
progress = video.getCurrentPosition();
}
}
#Override
public void onResume() {
super.onResume();
if(video.isPlaying() && progress!=0) {
video.seekTo(progress);
video.start();
}
}
How to keep video playing after screen rotates?

Android: Videoview lose the pause state after change orientation

i'm playing audio with Videoview. If try to change orientation during the player in pause state, the player loses this state and continues to play.
I have try to stop it but not succeeded.
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("pos", mediaView.getCurrentPosition());
outState.putBoolean("playerState", mediaView.isPlaying());
}
and on restore
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
boolean playerState = savedInstanceState.getBoolean("playerState");
if(playerState) {
mediaView.start();
} else {
this.posOfMedia = mediaView.getCurrentPosition();
mediaView.pause();
}
}
has anybody an idea how can I keep pause state after rotation

Categories

Resources