【问题标题】:Alert dialog dismissed while rotating phone in android在android中旋转手机时警报对话框消失
【发布时间】:2013-04-05 15:32:49
【问题描述】:

我已在清单中为我的应用程序中的所有活动授予所有权限,但我的活动在旋转手机时重新开始。重新启动会关闭我的警报框,再次调用 Web 服务器以获取数据和活动确实保持以前的状态。我已经尝试了所有的可能性,但无法得到解决方案。我该如何解决这个问题?

@Override
public void onCreate(Bundle savedInstanceState) 
{       
    super.onCreate(savedInstanceState);  
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.station_list_layout);

    ConstantValues.CURRENT_ACTIVITY=StationListActivity.this;

    ConstantValues.NEAREST_PLACE_MENU_SLIDER_FLAG=false;
    ConstantValues.MESSAGE_MENU_SLIDER_FLAG=false;
    ConstantValues.STATION_LIST_MENU_SLIDER_FLAG=true;

    orientation=getResources().getConfiguration().orientation;
    userAndMessageCount=new UserAndMessageCount();
    ((TextView)findViewById(R.id.stationlist_route_name)).setText(ConstantValues.ROUTE_NAME);
    list = (ListView) findViewById(R.id.myListView);  
    Cursor cursor=new UpdateLocalDatabase().getStationNameByRoute();
    adapter=new StationListAdapter(StationListActivity.this, cursor);
    adapter.notifyDataSetChanged();

    if(!ConstantValues.STATION_LIST_LOATED)
        userAndMessageCount.execute();
    else
    {
        Footer.setMsgCount(ConstantValues.MSG_COUNT);
        Footer.setUserCount(ConstantValues.FAVORITE_STATION_LIST.size());
        list.setAdapter(adapter);
    }

} 
         <activity 
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="unspecified"
            android:name=".LaunchingPageActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>`

【问题讨论】:

标签: android


【解决方案1】:
android:configChanges="keyboardHidden|orientation|screenSize"

将此添加到清单中的活动元素

喜欢

<activity 
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:screenOrientation="unspecified"
            android:name=".LaunchingPageActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>`

【讨论】:

  • @SathishKumar screenSize 属性仅在您为 API 级别 13 或更高级别进行开发时才需要。..
  • 您需要针对最新的 API 级别,这是最佳实践
【解决方案2】:

您可以尝试使用 onSaveInstanceState() 和 onRestoreInstanceState() 方法保存和恢复 Activity 的状态。

请参阅here 了解更多信息。

见问题here。

编辑 例如: 如果您使用的是 webview,您可以执行以下操作:

@Override
protected void onSaveInstanceState(Bundle outState )
{
    super.onSaveInstanceState(outState);
    web.saveState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
   super.onSaveInstanceState(savedInstanceState);
   web.restoreState(savedInstanceState);
} 

我建议阅读:Android - Preventing WebView reload on Rotate
另请阅读:How to prevent WebView auto refresh when screen rotation

【讨论】:

    【解决方案3】:

    这是正常行为。来自关于处理运行时更改的 android 文档:

    “某些设备配置在运行时会发生变化(例如屏幕方向、键盘可用性和语言)。当发生这种变化时,Android 会重新启动正在运行的 Activity(调用 onDestroy(),然后调用 onCreate())。重新启动行为旨在通过使用与新设备配置匹配的替代资源自动重新加载您的应用程序来帮助您的应用程序适应新配置。”

    见here

    【讨论】:

      【解决方案4】:

      这不是问题,因为需要重新绘制视图,重新启动活动以根据新方向进行绘制..这是根据 Android 文档.. 无论如何,您可以在清单中注册方向更改 onconfigChanges 并自行处理..

      【讨论】:

      • 我该如何处理,因为我的应用程序从网络获取数据。旋转手机时,它再次调用网络以获取数据,并且警报框也被关闭。很多麻烦,我该如何解决。请给点意见谢谢
      【解决方案5】:

      添加

      android:configChanges="orientation|keyboardHidden"

      到您的相应活动,这将解决您的问题。

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多