문제

So I have an Activity that handles all my web-service calls. Whenever an activity needs information from the internet I start it and it runs the download code while displaying a loader for the user.

My problem is that if the user changes orientation (portrait to landscape or vice versa) the activity that requested the download is destroyed and recreated (as is any other activities in the backstack i assume?). Ive added the android:configChanges="orientation|screenSize" to my loading-activity to ignore the orientation-changes for the loading-activity but first of all it doesnt seem to work (the orientation changes anyway?) and the calling activity is also destroyed and recreated (causing it to start a new loading-activity requesting results).

What I want to achieve is to stop any orientation-changes while the loading-activity is the active one.

EDIT: My activity in the manifest:

<activity
android:name=".LoadingActivity"
android:theme="@style/LoadingTheme"
android:configChanges="orientation|screenSize" >

And my activity (LoadingActivity) have overriden:

 @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
      //ignore orientation change 
     Log.d("LoadingActivity","Ignoring config change");
      super.onConfigurationChanged(newConfig); 
    } 

I noticed that the LoadingActiviy isnt actually destroyed but only the other Activities in the backstack.

I solved it by creating a boolean in all activities using LoadingActivity called WaitingForResults, that way when the activity is created, if its true I dont make another call to retrieve data from the server (because LoadingActivity will correctly deliver the result to the re-created activity).

도움이 되었습니까?

해결책

If you already specified in the manifest that you will handle the orientationChanges, and you don't want the activity to go through the regular life cycle, seems like all you are missing is to override this method in your activity as shown below:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(null);
}

Hope it Helps!

Regards!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top