0

I'm facing this strange issue: I'm using startActivityForResult() to pass to the next Activity. In the second Activity everything is fine until I start changing the phone orientation, which cause some problem when going back to the previous Activity.. it seem the returned Intent is null

I tried to use onSaveInstanceState()/onRestoreInstanceState() but can't get it to work.

Any ideas?

Update

this is the code used to send back data which works fine if I don't change the orientation

// photosList is an ArrayList<String>
Intent resultIntent = new Intent();
resultIntent.putStringArrayListExtra("photos", photosList);
setResult(RESULT_OK, resultIntent);
ColdFire
  • 6,764
  • 6
  • 35
  • 51
  • I am assuming its happening due to the fact that Android reloads the Activity when orientation changes. But I'm confused as to why its not working for you. The Intent sent back should not be null. Could you please post the code relevant to the Intent. – Andy Aug 16 '12 at 23:16

4 Answers4

0

Depending on what your activity does, you probably don't need to have your Activity reloaded when your orientation changes. You can add this to the corresponding <Activity> section of your AndroidManifest to disable the reload:

android:configChanges="screenOrientation"

This essentially tells the device that you will handle config changes yourself, and you can do so via callback, or you can ignore it if you dont care about orientation changes.

Nick
  • 8,181
  • 4
  • 38
  • 63
  • this doesn't fix the problem.. btw the way I had a similar problem when call the camera Intent and I fixed it by using `onSaveInstanceState()/onRestoreInstanceState()` but it didn't work for this activity – ColdFire Aug 16 '12 at 23:29
  • which activity are you applying this parameter to? should be the invoked activity, not the invoking activity. – Nick Aug 16 '12 at 23:29
  • I applyed it for the first activity (the one making the startActivityForResult() call) – ColdFire Aug 16 '12 at 23:30
  • thats the wrong activity...put it in the activity being started. – Nick Aug 16 '12 at 23:32
  • also are you calling finish() immediately following setResult()? – Nick Aug 16 '12 at 23:33
  • finish() isn't a problem since I'm the intent is instantiated within `onStop()` – ColdFire Aug 16 '12 at 23:36
  • gotcha - didnt realize that was what you were doing. – Nick Aug 16 '12 at 23:38
  • the error started to occur again.. I guess [Andy](http://stackoverflow.com/users/1118919/andy) was right in his last comment.. it has something to do with code being called within onStop()..some data may become null.. I overrode finish() and it seems to work fine – ColdFire Aug 17 '12 at 00:01
  • There are other configuration changes which might lead to the activity being recreated. Hence in general this is just the wrong suggestion, as it just hides the problem most of the time. – sstn May 07 '15 at 07:25
0

Ok, so the above answer works to an extent, but you should not be doing what you are doing on onStop. The problem you are getting seems to be coming from the fact that you need to save the extras you get from the calling Activity. I am going to assume what you are sending it will never really change, and you can expect it to always send the same amount of extras, but dynamic data in them.

@Override
onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    Intent intent = getIntent();
    savedInstanceState.putStringArrayList("photos", intent.getStringArrayListExtra("some_string"));
}

Then in onCreate you can do something like this:

onCreate(Bundle savedInstanceState) {
    /*I assume you got stuff places so just fit it in somewhere nice*/
    Intent resultIntent = new Intent();
    if(savedInstanceState == null) {
        //we know here that this is the first call to the second Activity
        //So you can just put the Intent that was sent since we know it came from the calling Activity with the original stuff
        resultIntent = getIntent();
    } else {
    resultIntent.putStringArrayListExtra("photos", bundle.getStringArrayListExtra());  
    }  
}

So hopefully you understand what I did there. It essentially gives you a way to have access to anything you need to persist beyond reload. Now you can do setResult(RESULT_OK, resultIntent); wherever it is you would have normally had it if the orientation issue had never existed. So as you see thats less code that having to override things you may only need for that. Let me know if it makes sense. Oh and DO NOT add anything to disable orientation if you do it like this :)

Andy
  • 10,553
  • 21
  • 75
  • 125
  • that can't be the problem since the code is called within `onStop()` beside the problem happens when changing orientation – ColdFire Aug 16 '12 at 23:35
  • 1
    Haha. Then in that case no wonder it isn't working. I'm not at my comp. So this is hard. But you solved your problem. But when I get a chance I will show you something that allows you to not have to stop the orientation reload – Andy Aug 16 '12 at 23:46
  • that sounds great.. i'll be greatfull – ColdFire Aug 16 '12 at 23:48
  • I guess you're right andy.. the problem occured again.. i think it happens because the code was called within onStop().. I overrode finish() it seems to be stable now.. was that your point? – ColdFire Aug 17 '12 at 00:05
  • No, you should not be doing that. You should be putting the info you get from the intent in `onSaveInstanceState`, then when it does its orientation change, you can either get it back from the Bundle returned by onCreate OR override `onRestoreInstanceState`. The reason this would work is because the the Intent that is returned by `getIntent()` may have no extras, but you can put them back from the Bundle you saved in `onSaveInstanceState`. I will show you an example. – Andy Aug 17 '12 at 04:17
  • like I said my Activity is already handling this orientation changes.. I think the problem is my code being called in the onStop() method which may cause data to be released before being sent, so I thought about putting it in finish() since it's the first method to be called when the activity is ending..so far the problem is gone – ColdFire Aug 17 '12 at 10:48
  • I understand that. But you should not be doing it there. If its not obvious like is your case, then you shouldn't be doing it there. The way I am giving you is honestly a much better way than putting it in on stop. Unless there's a reason why its needs to be there? Above you edited that you tried it my way? Has it really not worked? – Andy Aug 17 '12 at 17:03
0

I had the same problem. It appears, that if you set the result code to Activity.RESULT_OK (-1), it gets reset to 0 on orientation change by Android. So you have to recreate your intent and reset the result code somewhere (perhaps in the overridden finish() method).

k29
  • 1,859
  • 1
  • 15
  • 15
0

Android recreates your activity when you change the orientation

That is may be the problem you are facing, try to store the data to Shared Preference and re assign the values to your variable in onCreate() method.

You can also try onsaveinstancestate() and onrestoreinstancestate() methods to save your activity state.

Darshan
  • 515
  • 1
  • 3
  • 16