Android AsyncTask context behavior

前端 未结 4 2034
轮回少年
轮回少年 2020-12-23 17:11

I\'ve been working with AsyncTasks in Android and I am dealing with an issue.

Take a simple example, an Activity with one AsyncTask. The task on the background does

4条回答
  •  情深已故
    2020-12-23 17:49

    This is the type of thing that leads me to always prevent my Activity from being destroyed/recreated on orientation change.

    To do so add this to your tag in your manifest file:

    android:configChanges="orientation|keyboardHidden" 
    

    And override onConfigurationChanged in your Activity class:

    @Override
    public void onConfigurationChanged(final Configuration newConfig)
    {
        // Ignore orientation change to keep activity from restarting
        super.onConfigurationChanged(newConfig);
    }
    

提交回复
热议问题