I display a splash screen for about 3 seconds before my first Activity is called. Splash Screen is also an Activity, after 3 seconds it finish()es and starts FirstActivity.
Also I have set screen rotation of splash Activity to portrait view.
Now when I test my app it works fine, but during those 3 seconds of splash screen if i change screen rotation, my First Activity is called two times.
Is there any way / code snippet that could help me open my First Activity only once, despite screen rotations when Splash Screen Activity is at foreground. ? Thanks for helping :)
It's not really a bug. When you change orientation the current activity is created again, hence why your SplashActivity is called twice.
I wrote a blog post about handling orientation changes manually; http://c0deattack.wordpress.com/2010/12/25/dealing-with-screen-orientation-changes-manually/
I also had this 'bug'. Here it describes how I solved it: Activity reloads when orientation changes in Android
I think your thread of splash screen gets call again one more time.
So simply set a flag to check screen rotated or not. If yes than do not call this thread again. eg.
boolean yourScreenRotationFlag=false;
if(!yourScreenRotationFlag)
{
// your splash thread code
}
and on rotation of screen set this flag to true.
you can solve it by add this xml attribute to the Splash Activity in the Mainfest
android:configChanges="orientation|keyboardHidden|screenSize"
it prevent your activity from Re-create when orientation occured
Related
My issue is when an Android Application goes in the background. When I click the Home button and launch my app again from the home screen by clicking the Application icon, it should display the the same screen from which I went to the Home screen. But it calls the onDestory() method then comes out of my application. I thought the application is killed by the system because of memory requirement etc., but I need tokeep the activity and it should again show the same screen where I left instead of starting all over again.
This may be achieved like maintaining sessions.
try putting
android:alwaysRetainTaskState="true"
in the androidmanifest.xml for those activities, i think ICS does that by default now.
The Application will show you the same screen when return after "home" button if your app with different screens is made of different activities to show the interface parts... But if you just make some objects
visible=true or false
so, after resume you'll see the first view... Try to use Intents between different activities... And show a piece of code to help you... Maybe the problem is in overriding of onDestroy, onPause, onResume methods
I have a lot ot async tasks in my activity. If screen rotation occurs - they gone. What to do to prevent asynctasks from trowing exception?
I think exception is due to the re starting of the activity in orientation changes, if use android:configChanges="orientation|keyboardHidden" for your activity in your manifest, which prevents restart
use
android:configChanges="keyboardHidden|orientation"
as a attribute of your Activity in AndroidManifest.xml file to prevent reload Activity on screen rotation .
The most proper way to this is to use a fragment to retain the instance of the async task, over rotations.
Here is a link to very simple example making it easy to follow and integrate this technique into your apps. This works brilliantly all buttons and images etc are redrawn as expected
https://gist.github.com/daichan4649/2480065
I want to make my application displayed only in portrait orientation, so I have put android:screenOrientation="portrait" in the Activity tag in AndroidManifest.xml, and have put setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); in the Activity's onCreate method.
This works to lock the orientation to portrait, however, when the Activity starts, it shows itself once, then shows itself again, so you see a sort of flash. I can confirm that onCreate is being called twice as well.
This flash is causing further force closes later in my application.
How can I eliminate this flash?
Edit
I had a splash screen displaying before the activity I had described. It was being run twice, and therefore ran this activity twice (via an Intent) twice. I fixed this issue by checking if this activity had already been stared using an intent in the splash screen class, and it had, not to run it again. The fix was more of a workaround than a fix, but I hope it helps people.
I believe that using the Activity's configChanges attribute (in the manifest) should solve your problem. ConfigChanges attribute documentation
However, you are just going around another problem. What you really should address is why is that provoking FC? There is absolutely no reason for that to happen so you also should solve that problem. More info in handling runtime changes.
Please note: using the first approach is acceptable to address the performance/UX issues though.
Just in case you wonder why onCreate is called twice, once I got into this problem and I think that it was related to having the orientation fixed and then having another activity being created but in a different orientation. Before the second activity was started, the former activity changed into the orientation of the latter. And remember that keyguard is also an Activity! I'm not sure if this happens for this reason though.
don't put : "setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);".
Just let the AndroidManifest do his job. I thing what you did is :
Tell your App to go only in Portrait mode
Tell your Activity to be in Portrait mode, wherever it was already like this or not (The flash have to come from this).
I could be wrong.
I want my app to be displayed only in Portrait mode. I
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);) as long as the app is launched in Portrait mode and then change it to landscape when the app is running.
The issue is, if I launch the app in Landscape mode, it displays the Splash Screen for a second and then goes all black and again displays the Splash Screen. this keeps going on forever and never proceeds to the next screen.
any help on resolving this issue is much appreciated.
thanks.
You can it inside the manifest on your activity
<activity android:name=".MainActivity" android:screenOrientation="portrait" />
Recheck your Manifest.xml - who is the launcher activity and who is the default.
also recheck which activity you call from the splash screen. maybe the name has changed for some reason.
If not, try using this answer: Android - switching between landscape and portrait mode makes Intent lose values
I don know what code have you written .. probably if you can share few snippet of code where you are calling splashscreen and after some timeinterval you call another activity ... we could provide you a solution.
I am using android HTC HERO 2.1 version.
The activity I write :
<activity android:name=".gogogo"
android:label="#string/app_name"
android:theme="#style/Theme.mine"
android:screenOrientation="landscape"
android:configChanges="orientation">
let my orientation change to landscape.
However, I figured out that every time I pressed "power" button and then come back to my activity, it always start at portrait.
I tried the game : TEETER , which was written not by me. Has the same problem too, any one know how to fix it??
Edit: it always start at portrait. --> I mean, if you come back from power, you have to scroll down to do everything, there is something like screen lock.
You can see that there is a "status bar" at the top of the screen and at this time, the orientation is "portrait". So after You scroll the "screen lock" a little bit down, you can see the activity (such as TEETER) is at "portrait" state. When you scroll all the way down, the activity's orientation will suddenly change to the state "landscape".
So the conclusion is : My activity is forced to orient once. And I really want to know how to fix it!
Hai Steven Shih:
Pls go through this page of developer guide.
http://developer.android.com/guide/topics/manifest/activity-element.html
and try this line
android:windowSoftInputMode="stateUnspecified|adjustPan"
Not sure if it's the same problem, but here is the problem I had :
I force the orientation as landscape in the manifest
My activity has a two surfaceviews ( a camera preview and a layer on top of it )
After pressing the power button and coming back, my views were suddenly in the wrong orientation
It seems to be the same problem as another question : Disable orientation but detect orientation
I think the problem is that once you press the power button, the lock screen forces the orientation as portrait. In the onConfigurationChanged I get an orientation change right when I press the power button, which was unexpected because I told to explicitly lock the orientation. The fact is that when another activity which comes in front forces the orientation, the other activities get notified of the orientation change, even if they asked for an explicit orientation.
Here's my workaround :
in onPause, detach the views with removeView ( my application has programmatically created views, not an xml layout )
in onResume , first check if the orientation is the one you requested ( getResources().getConfiguration().orientation ). If it's the case, reattach the views ( addContentView or whatever ).
if the orientation is not the one you expect, call setRequestedOrientation
onConfigurationChanged will be called to tell the orientation has been changed to the one you requested. At this point you can reattach the views with setContentView.
Seems a bit complicated, but that's what I had to go through in my particular case, since my views are created dynamically and directly attach to the default FrameLayout of the activity. Maybe I then missed some default behaviors because of that, but this way I could achieve the reactivity I was looking for.
Not sure whether this can be called a fix, but it's kind of a work-around.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus)
setContentView(mGameView);
else
setContentView(mEmptyView);
}
Where mGameView is your ordinary content and mEmptyView is just a simple View object. The activity will still be drawn in portrait for a frame or two, but it will not be visible to the user.
I've tested this with HTC Hero and Nexus S.