Activity restart on rotation Android

前端 未结 30 3985
花落未央
花落未央 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.

提交回复
热议问题