Activity restart on rotation Android

前端 未结 30 3839
花落未央
花落未央 2020-11-21 04:32

In my Android application, when I rotate the device (slide out the keyboard) then my Activity is restarted (onCreate is called). Now, this is proba

相关标签:
30条回答
  • 2020-11-21 05:05

    Instead of trying to stop the onCreate() from being fired altogether, maybe try checking the Bundle savedInstanceState being passed into the event to see if it is null or not.

    For instance, if I have some logic that should be run when the Activity is truly created, not on every orientation change, I only run that logic in the onCreate() only if the savedInstanceState is null.

    Otherwise, I still want the layout to redraw properly for the orientation.

    public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_game_list);
    
            if(savedInstanceState == null){
                setupCloudMessaging();
            }
    }
    

    not sure if this is the ultimate answer, but it works for me.

    0 讨论(0)
  • 2020-11-21 05:05

    It is very simple just do the following steps:

    <activity
        android:name=".Test"
        android:configChanges="orientation|screenSize"
        android:screenOrientation="landscape" >
    </activity>
    

    This works for me :

    Note: orientation depends on your requitement

    0 讨论(0)
  • 2020-11-21 05:05

    Even though it is not "the Android way" I have gotten very good results by handling orientation changes myself and simply repositioning the widgets within a view to take the altered orientation into account. This is faster than any other approach, because your views do not have to be saved and restored. It also provides a more seamless experience to the user, because the respositioned widgets are exactly the same widgets, just moved and/or resized. Not only model state, but also view state, can be preserved in this manner.

    RelativeLayout can sometimes be a good choice for a view that has to reorient itself from time to time. You just provide a set of portrait layout params and a set of landscaped layout params, with different relative positioning rules on each, for each child widget. Then, in your onConfigurationChanged() method, you pass the appropriate one to a setLayoutParams() call on each child. If any child control itself needs to be internally reoriented, you just call a method on that child to perform the reorientation. That child similarly calls methods on any of its child controls that need internal reorientation, and so on.

    0 讨论(0)
  • 2020-11-21 05:05

    People are saying that you should use

    android:configChanges="keyboardHidden|orientation"
    

    But the best and most professional way to handle rotation in Android is to use the Loader class. It's not a famous class(I don't know why), but it is way better than the AsyncTask. For more information, you can read the Android tutorials found in Udacity's Android courses.

    Of course, as another way, you could store the values or the views with onSaveInstanceState and read them with onRestoreInstanceState. It's up to you really.

    0 讨论(0)
  • 2020-11-21 05:07

    Changes to be made in the Android manifest are:

    android:configChanges="keyboardHidden|orientation" 
    

    Additions to be made inside activity are:

    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:09

    I just discovered this lore:

    For keeping the Activity alive through an orientation change, and handling it through onConfigurationChanged, the documentation and the code sample above suggest this in the Manifest file:

    <activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">
    

    which has the extra benefit that it always works.

    The bonus lore is that omitting the keyboardHidden may seem logical, but it causes failures in the emulator (for Android 2.1 at least): specifying only orientation will make the emulator call both OnCreate and onConfigurationChanged sometimes, and only OnCreate other times.

    I haven't seen the failure on a device, but I have heard about the emulator failing for others. So it's worth documenting.

    0 讨论(0)
提交回复
热议问题