Android application seems to be recreated after camera launch - android

I am launching camera intent from my android application using this answer (I need user to be able to choose image from camera or from gallery)
But it seems that my whole app is recreated - activity's onCreate is called, and one of the classes, that is a singleton, has toString() value, that differs from the value before camera launch.
Here is also my activity description from android manifest:
<activity
android:name="xxx"
android:theme="#style/NoActionBarTheme"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name">
</activity>
Any ideas what is going wrong?

Make sure uncheck "Dont Keep Activities" from Developer Options in Settings.
Also check your manifest and code if your activity has correct launchMode.

On some devices(i observed samsung s3), when launch camera app, device orientation is changed to landscape & again to portrait when select captured photo to return to our app. Our activity is re-created again & we end up as just started activity.
In this case, you have to implement following pair of callbacks for activity. OnSaveInstanceState() & OnRestoreInstaceState().
You can save member variables & restore them back when come back to your activity from camera app.
e.g. imagePath (location you give for saving camera picture) can be saved & restored.

Related

react native android: How to save state on screen rotation or orientation

On rotating screen, it seems activity is getting restarted, it loses all it's state. I checked android developer guide.Handling Runtime Changes. It has mention about how to handle screen orientation and using onSaveInstanceState() before it destroys your activity and restore the state during onCreate() or onRestoreInstanceState().
I am answering to my question this is how it worked for me, if anyone is facing same problem, then edit your AndroidManifest.xml on
android/app/src/main and
local-cli/generator-android/templates/src/app/src/main
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
Here is a detailed explanation of how to save/retrieve activity instance in Android
https://web.archive.org/web/20160308105435/http://www.intertech.com/blog/saving-and-retrieving-android-instance-state-part-1/

android orientation - issues with intents (camera)

Here is an interesting one...
I have an application I am writing for devices running android 2.3.3 and above.
It has a main activity which calls the camera via an Intent.
If the user clicks a button to launch the camera; then takes a picture; then clicks "Done" to return to the main activity - the application works fine and displays the new picture in an imageview on the main activity.
however, if the user uses the main activity in portrait orientation; then clicks the button to open the camera intent and changes the orientation to landscape; then click done to return to the main activity - the application crashes
but then...however, if the user uses the main activity in portrait orientation; then clicks the button to open the camera intent and changes the orientation to landscape; then changes the orientation back to portrait (in the camera) before clicking done to return to the main activity - then the application continues to run normally.
Stuff I have tried:
I have changed the manifest file to force the application (main activity) to be oriented in portrait (I have also removed this)
I have added this line to the main activity in an attempt to handle the re-drawing of the activity on it return from the camera intent:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
In the debug window I get errors describing nullpointer exceptions - I think this is because the views are no longer there after an orientation change for the image to be passed into unless the orientation is the same as when it was left.
I'm a bit stuck so would appreciate some advice.
When your screen orientation changes, your Activity is destroyed and recreated in the new orientation. So any variables that gained a reference during the life of the Activity will no longer refer to anything, and if you then try to access objects they refer to without re-assigning them values, you'll get a NullPointerException.
The method onSaveInstanceState() is used to save temporary data between configuration changes. This creates a Bundle which is passed to onCreate() when the Activity starts up again.
Without any code, I don't know if that's your problem, but it's worth a look.
See http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges for more information (and more accurate information than I've provided, no doubt).
hey i am not sure but try to put this property in manifest file hopefully it should work
android:configChanges="orientation|keyboardHidden"
Some Android devices like Samsung S3 and S4 have default camera surface view in Landscape mode.
When you call camera and capture image and come to your application, whether you had set orientation PORTRAIT it will first open in LANDSCAPE mode and then changes to PORTRAIT mode.
Its Android OS behaviour.
Due to this Activity Re-Creates it self. At that time you are getting Null Pointer Exception.
You can handle by setting configChanges in menifest file.
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
and you can store the data temporary in onSaveInstanceState() rightly said by #Spinner in his answer.

Android screen orientation change have different behaviour on tablet

I have activity with:
android:screenOrientation="portrait"
and code:
if(blah blah blah)
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
in activity onCreate.
1. When test on smartphone "setRequestedOrientation" causes destroy activity and create again with desired screen orientation.
2. When test on tablet device this code runs only once activity. After "setRequestedOrientation" it just continues without destroy and create again activity. This happens when start code without debuger or without breakpoint in onCreate. If I stop in onCreate function - I see in logs - destroying and onCreate again.
How to catch specific situation with tablet?
UPDATE:
- Problem with tablet occurs when 'Orientation lock' if off. If 'Orientation lock' is enabled program works in same way as in smartphone.
UPDATE2:
- Is it possible to set preffered screen orientation to whole application. So when activity starts to know desired orientation. This will make unnessesary call 'setScreenOrientation' and avoid second activity creation.
After whole day spent with this issue I made following changes in my application:
1. Every activity /without first/ are declared as android:screenOrientation="behind".
2. In first activity I set preferred screen orientation using user selected mode /from my menu.
3. I call setRequestedOrientation only when user changed mode in my menu options.
This causes activities to be created only once. Finaly :)
No.. you cannot set preffered screen orientation for whole application... instead you should call for setScreenOrientation in respective activies only... Refere this link here...
You can set
<activity ActivityName = "test_activity"
android:configChanges="orientation">
</activity>
in your android manifest file. It will not allow activity to restart on orientation change but onconfigurationchange will be called normaly.
Later you can getdisplayorientation in onconfigurationchange and set whatever you want.

Android app using camera with android:screenOrientation="landscape"

My app is screen_orientation_portrait,but when I opened camera,the picture in my camera is 90degreed rotated.
So I tried to add "android:screenOrientation="landscape",in demo ,it is ok,but when I use it in my app,the camera activity opened and the app turned to be screen_orientation_landscaped,and the camera activity closed.
I reallied it is because when change the screen_orientation,the activity is going to reopen it self,but in my app,it dose not open after closed.What I can see is my ActivityGroup.
So can I just made my camera activity not reopen after the orientation is changed?
add this to your activity in manifest
android:configChanges="orientation|keyboardHidden"></activity>

Activity restarts when rotating Android phone

My activity restarts when I rotate my phone.
How can I fix this problem?
add
android:configChanges="orientation"
to your Activity in AndroidManifest.xml.
Save your non-view instance state in onSaveInstanceState or in onRetainNonConfigurationState and restore state in onCreate. Save "shared document-like data" in onPause() by writing to a database and restoring "shared document-like data" in onResume().
Also see the docs.
There are various posts on this, but for Android Version 3.2 and newer you should add android:configChanges="orientation|screenSize" to the activity in your AndroidManifest.xml, as the screen size changes between landscape and portrait orientation.

Categories

Resources