GetIntent () when changing orientation - how to handle this?

This is similar to the noob question, but in Android, when the orientation changes activity, it is destroyed and recreated, and the onCreate () function is called again. The internal state can be saved through the savedInstanceState package. So far so good.

But in my application, I have an activity that is usually called from another action via startActivityForResult (), which is passed to a bunch of parameters. In onCreate (), the target activity does getIntent () to retrieve the transferred data. The problem is that when this action is displayed on the screen and I flip the orientation with which it falls. It crashes in onCreate () code, where it calls getIntent (). I assume that it crashes because there is nothing to “get”, because in this case it is called from the system and not from another activity.

What is the right way to handle this? How do I know when my onCreate () is called due to a change in orientation, so I'm not trying to call getIntent ()? Or am I just thinking about it wrong? Thank you in advance.

+7
source share
1 answer

You can save the data obtained using the getIntent() method in the getIntent() file. This means that in your onSaveInstanceState(Bundle) save the information you use in onCreate() using getIntent() . When your activity newly created, in the onCreate(Bundle savedInstanceState) method, the onCreate(Bundle savedInstanceState) package is null. That way, you can make a validation condition in your onCreate(Bundle savedInstanceState) if the Bundle savedInstanceState is null, which means you need to get the data using getIntent() else if its value is not null, which means it was some kind of change in oritentation, and you need to get the data using savedInstanceState package.

+8
source

All Articles