Android Question p.setscreenorientation ?

tufanv

Expert
Licensed User
Longtime User
Hello,

I need to show a graph at a different panel when clicked on a button. To show it better , the graph must be shown on a landscape panel but my app is normally using portrait mode.

Problem is , when the button is clicked the main panel which is portrait is hidden and panelgraph comes and i set screenorientation there but this restarts the app. I think it is the normal behaviour but this makes a problem for me.

Is there a way to solve this issue to successfuly change orientations without any restart ?

Thanks
 

Cableguy

Expert
Licensed User
Longtime User
it is the normal behaviour, on screen rotation, the app will pause, then resume...
I would suggest setting a flag to know if the rotation is called from the code of from user rotation, then set it on app pause, and check it on resume.**I also recall there was a module that was meant to manage the pause/resume states... but cant recall the name
 
Last edited:
Upvote 0

moster67

Expert
Licensed User
Longtime User
You could try by inserting the following attribute in the manifest:
B4X:
SetActivityAttribute(Main, android:configChanges, "orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation")
Above attribute will make the code skip the onCreate method upon orientation-change.

If you need to detect when an orientation change takes place you could try out the following code:
B4X:
Sub Activity_ConfigChanged()
   
    '****************************************************************************************
    'This event detects a configuration-change
    '****************************************************************************************
   
    Log("inside configsub")
End Sub

#if JAVA

//This java-code will detect a configuration-change. We use it to trigger our event
//Activity_ConfigChanged in our B4A-code.

import android.content.res.Configuration;
import anywheresoftware.b4a.keywords.Common;
import android.view.WindowManager;
import android.content.Context;

@Override
public void onConfigurationChanged(Configuration newConfig) {

    super.onConfigurationChanged(newConfig);
    processBA.raiseEvent(null, "activity_configchanged"); // trigger event in B4A-code                                                      
}

#end if

You can check this project for some more information
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
You could try by inserting the following attribute in the manifest:
B4X:
SetActivityAttribute(Main, android:configChanges, "orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation")
Above attribute will make the code skip the onCreate method upon orientation-change.

If you need to detect when an orientation change takes place you could try out the following code:
B4X:
Sub Activity_ConfigChanged()
  
    '****************************************************************************************
    'This event detects a configuration-change
    '****************************************************************************************
  
    Log("inside configsub")
End Sub

#if JAVA

//This java-code will detect a configuration-change. We use it to trigger our event
//Activity_ConfigChanged in our B4A-code.

import android.content.res.Configuration;
import anywheresoftware.b4a.keywords.Common;
import android.view.WindowManager;
import android.content.Context;

@Override
public void onConfigurationChanged(Configuration newConfig) {

    super.onConfigurationChanged(newConfig);
    processBA.raiseEvent(null, "activity_configchanged"); // trigger event in B4A-code                                                     
}

#end if

You can check this project for some more information

Hello this makes a problem. only half of the screen is shown like the portait. Please see the topic i opened before i realize that the manfiest code I added from the above reply makes this error:,
https://www.b4x.com/android/forum/threads/landscape-shows-only-the-half-of-screen.78719/
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
I don't know if what I suggested is the correct solution or not. Maybe it would be easier to show the chart in landscape mode in a separate activity instead of in a panel?

You may need to adjust the size of the panel when changing into landscape mode. As I wrote, when using the suggested attribute, the OnCreate method is not executed and B4A does not know the height and width values. In order to see if you can fix that, see the project to which I linked to in my earlier post. In that project you can find code how to re-calculate height/width.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

tufanv

Expert
Licensed User
Longtime User
This will probably be the best option.

Setting the orientation of a single activity: https://www.b4x.com/android/forum/t...-of-an-activity-reflection.52169/#post-326665
this solution works well for me but the problem is

1)when i go back to main activity not only the activity resume is executed but also activity_create is executed . Is it possible to bypass the create and just execute activity_resume ?

2) when i use a button on my second activity to turn back to first I use code :
B4X:
    StartActivity(Main)

this opens the first activity. While in the first activity again if i click back button of my phone it closes the main activity and reopens the second activity. So i figured that i have to use avtivity.close while turning back to main activity from the second one. But that also does not solve the problem. When i click back on the main activity after coming back from the second one , if i click phone's back button it opens the second activity again.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1) Have you set the main activity orientation to portrait? If so then try to add the SetApplicationAttribute from the second post (not the other code).

2) You should call Activity.Finish. It will remove the second activity from the activities stack. Pressing on the back key will now remove the main activity and should exit the app. Make sure that you are not handling the back key in your code.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
1) Have you set the main activity orientation to portrait? If so then try to add the SetApplicationAttribute from the second post (not the other code).

2) You should call Activity.Finish. It will remove the second activity from the activities stack. Pressing on the back key will now remove the main activity and should exit the app. Make sure that you are not handling the back key in your code.
yes i have. After adding the manifest in post 3 ,
The code mainly works but there is a little problem. 4 out of every 5 try works perfect, activities close and open as exptected but in 1 of every 5 try (nearly) when switching to second activity, sedcond activity does not come up , first activity turns to landscape and only half is visible. I don't know what causes it . if it happens everytime i can find the problem but i cant find this way.[/QUOTE]
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
1) Have you set the main activity orientation to portrait? If so then try to add the SetApplicationAttribute from the second post (not the other code).

2) You should call Activity.Finish. It will remove the second activity from the activities stack. Pressing on the back key will now remove the main activity and should exit the app. Make sure that you are not handling the back key in your code.
please watch this video about what happens :

 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Still makes the problem. again not everytime but 1 of 3-4 tries makes the problem. I call a sub named callchart with callsubdelayed and that sub includes startactivity.
Try to start the second activity by calling a sub with CallSubDelayed (it can be an empty sub).
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
I don't know why but when the app goes to second activity it was pausing it self and returning to first activity sometimes. I don't know if it is a good way but i solved the issue by placing
B4X:
If UserClosed=False Then
    StartActivity(Me)
End If

this way if the activity closes without user request ( which in my case closes itself sometimes an returns to first activity), the second activity restarts and do what it has to do )
 
Upvote 0
Top