android: application crashes if orientation changes in a subactivity - android

My main activity handles changes into portrait mode just fine. However, if I launch a very simple activity via startActivityForResult() and switch to portrait mode before finishing it, my main activity crashes because some of its variables are set to null.
It looks like my connection to the Service is not being recreated. I tried manually calling doUnbindService(); doBindService(); inside of onActivityResult() but onServiceConnected() is still not being called.
What's the proper way to handle the Activity lifecycle here?
EDIT: I see in the log file that onServiceConnected() is indeed being called -- but it's being called after onActivityResult().

For you second Activity add this
if your android:targetSdkVersion="12" or less
android:configChanges="orientation|keyboardHidden">
if your android:targetSdkVersion="13" or more
android:configChanges="orientation|keyboardHidden|screenSize">
in manifest.xml

The problem was that I was referring to objects which don't exist until onCreate() is called again. I needed to queue my answers locally and wait for those objects to be recreated to avoid crashes.

Related

When I launch an activity from main, onCreate() of main is called, and when the launched activity is finished onDestroy() is called

This is how I'm launching my activity:
Intent intent = new Intent(Quotr.this, AddQuote.class);
startActivityForResult(intent, ADD_QUOTE_REQUEST);
And that activity finishes with a call to finish()
But I've been having issues with my onCreate() and onDestroy() methods being in the main activity (defined in Quotr.class) being called when I don't want them to. I added calls to Log.v() in both of those methods to see what was being called and when, and oddly, when the activity is launched, onCreate() of the main activity is called, and when the new activity is finished, the main activity's onDestroy() method is called.
This is causing a ton of issues, and I don't even know how to work around it at this point, because all my cleanup in main activity's onDestroy (which I only expect to be run when the activity is ACTUALLY recreated, not every time this new activity is launched) is being called while the activity is still in use.
Oddly enough, this only happens with this particular activity. As far as I know, I'm calling and finishing all my other activity intents in the exact same way, but they don't mess with the main activity's life cycle like this AddQuote.class one does.
I'm really at a loss for what else I can check at this point. I've checked for anything wrong in the manifest, I've checked I understand activity lifecycles, but this really makes no sense.
Oh my god, so I'm a huge idiot.
I'm working on an app that's been slowly built up over the course of a year and a half, and it was my first ever attempt at an app. For some reason, for the first new activity I made in the app (the one I was having an issue with), I was extending the main activity instead of AppCompat activity. So my calls to super.onCreate() and super.onDestroy() were calling back to the main activity, because THAT'S the super class.
I'll leave this up anyway in the off chance someone makes this mistake as well. Hopefully it helps someone.

OnConfigurationChanged followed by OnStop ()?

I am stuck in a strange situation....
I have declared android:configChanges="orientation|keyboardHidden" in the manifest file of the Activity. So, ideally I want my Activity onConfigurationChanged() to be called whenever, I rotate the device. But, that does not happen.
Inspite of having these attributes in the manifest file, the Activity onStop() is called first followed by onCreate, (the onConfigurationChanged() is never called) when I rotate the device ----- This is the issue I am facing.
My expectation is --- Whenever, I rotate the device, onConfigurationChanged () to be called first, then onStop() and then onCreate().
What is the mistake that I am doing?
Is this at all possible ?
No, not possible.
You put android:configChanges="keyboardHidden|orientation|screenSize"in your manifest to handle configuration changes yourself.
You override the onConfigurationChanged() method and take care of configuration changes there.
onStop() and onCreate() are never called again, only onConfigurationChanged() is called during a configuration change in this case because the activity is never killed.

Preventing from onDestroy being called screen off button pressed

I have Activity that gets populated with info. When I press the screen off button and come back to the Activity, the info is not there anymore.
Btw, If I use the home button the info doesn't disappear. On logs, it shows it doesn't call onDestroy;
Therefore, onDestroy discards the info. I don't want onDestroy. HOw could I make that happen.
Thanks.
OnDestroy will always be called. You should save data and than recreate Activity on onCreate. Everything is nicely explained here. Read section "Saving Persistent State". Also read about lifecycle of Activity.
Try to save activity's info on onSaveInstanceState() and restore the state on onRestoreInstanceState().
This approach also helps you also in configuration changes state.
onSaveInstanceState() is called before destroying activity and onRestoreInstanceState() is called just before recreating the activity.
Follow this for more details.
Probably duplicate of Android activity onDestroy() called on screen lock.
This behaviour usually occurs at landscape orientation activities. Add
android:configChanges="orientation|keyboardHidden|screenSize"
to your AndroidManifest.xml activity tag.

abnormal behavior of parent activity after finishing child activity

I have Activity A and I am calling Activity B from Activity A using setResultForActivity.
Now in Activity B when I press Done button I am firing finish() and it returns to
Activity A and it return down to onActivityResult. Now the issue is after when I fired finish() in Activity B , Activity A's onCreate doesn't get called and thats why
some of the custom listeners in my ListView isn't working , it seems that they are not bind.
so the whole activity respond pretty weirdly , can anyone has solution to this ?
Why a fourth answer? Because in my view, the others aren't fully correct.
Truth is, Activity A could have been destroyed in the meantime, or not. This depends on whether Android needs memory or not. So it is possible that Activity A´s onCreate() is called (along with the other lifecycle callbacks), or not. In the latter case, onActivityResult() is called before onResume().
While for configuration changes, the most efficient way to preserve the Activity's state is via nonConfigurationState, if you want to prepare for a restart of your Activity after it has been destroyed, you can use the InstanceState mechanism, because while Android destroys your Activity A, it will keep its intent and saved instance state to re-crearte it.
This stresses the absolute necessity to place initialization exactly in the callback where it belongs.
To test whether your Activity logic works regardless of whether Android destroyed it or not, you can use the DevTools setting "Development Settings" -> "Immediately destroy activities". The DevTools app is available on AVDs and can also be downloaded from Google Play.
Just place your onCreate() stuff in onResume() of Activity A except setContentView().
Just have a read on Android Activity Lifecycle : http://developer.android.com/training/basics/activity-lifecycle/stopping.html. onCreate() is only called when the activity is first created. You can do your list thing in the onResume() method.
Activity A's onCreate won't get called because the activity has not been destroyed. When an Activity regains focus from another activity, it's onStart and onResume get called, so I would put your bound listeners in those. They will also be called when onCreate is normally called.
After your child activity is finished() it return to execute onActivityResult which is in your case in Activity A. The onCreate method is not supposed and does not get called when killing of you sub-activity, a.k.a Activity B.
Please post some source code for us to work on and I will improve my answer! :)

android: unusual way to start activity

In one activity I need to start another one but with one condition: if it was started before then it must be finished and started again. This activity show some information about system state (Theme.Dialog style) it also can start some services and so on. As far as I know when I do startActivity(intent) then onResume() will be called (if activity was started before). Does anybody know how to do it?
That's precisely how it should work. If you need to adjust the values on-screen, put that code in onResume(). What may not be obvious from its name is that onResume() is called when the Activity is first created as well. It's always the last method called before an Activity becomes active.
use the NEW_TASK_LAUNCH flag in the startActivity() call. Read documentation http://developer.android.com/guide/appendix/faq/framework.html#4
In the manifest file, in the activity attributes, you have the attribute launch mode, which let you specify how the activity must be launched (http://developer.android.com/guide/topics/manifest/activity-element.html#lmode).
Take a look at the description to see which mode suits most your needs. Then when the activity is brought to front, you can restart your service by overriding the Activity.onResume() method.
In one activity I need to start another one but with one condition: if it was started before then it must be finished and started again
There is nothing that only does this. The closest is to have the combination of FLAG_ACTIVITY_CLEAR_TASK|FLAG_ACTIVITY_NEW_TASK, but that has other side effects, such as wiping out any other activities.
As far as I know when I do startActivity(intent) then onResume() will be called (if activity was started before).
Not by default. By default, a second instance of the activity is created.

Categories

Resources