屏幕方向更改后片段重置

时间:2014-09-11 02:42:43

标签: android android-fragments

我有一个MainActivity,可以在onCreate中加载一个片段。我有另一个片段,我从操作栏加载。如果我在第一个片段上更改屏幕方向,它将按预期工作。如果我更改第二个片段,它将重置为第一个片段。这似乎是因为它重建了所有内容并将原始片段放回到片段管理器中。我确信有办法跟踪当前显示的片段,但我不确定如何。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Database entries
    myDataSource = new FillupDataSource(this);
    myDataSource.open();

    // Create an instance of the MyListFragment 
    MyListFragment firstFragment = new MyListFragment();
    Bundle bundle = new Bundle();
    bundle.putSerializable("dataSource", (Serializable) myDataSource);
    firstFragment.setArguments(bundle);

    // Add the fragment to the 'fragment_container' FrameLayout
    getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();

    getActionBar().setDisplayHomeAsUpEnabled(true);
}

我确定这必须是已经完成的事情,但我很难知道如何搜索它。

3 个答案:

答案 0 :(得分:2)

可能的解决方案是使用实例状态保存当前片段索引:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
   //save current fragment index
}

然后恢复状态

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    //have the activity switch the fragment via the saved fragment index
}

您可以在this SO post.

了解有关实例状态的更多信息

答案 1 :(得分:1)

在极端情况下,当方向更改时您不需要应用程序更新资源,您可以在清单文件中使用以下语句:

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

如果您正在为Android 3.2(API级别13)或更高版本开发,则需要:

android:configChanges="orientation|screenSize"

然后在您的Activity中覆盖onConfigurationChange方法:

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        //do something...   
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
        //do something...
    }
}

开发者指南中的注意事项:

  

自行处理配置更改会使使用备用资源变得更加困难,因为系统不会自动为您应用这些资源。当您必须避免因配置更改而重新启动时,此技术应被视为最后的手段,并且不建议用于大多数应用程序。

答案 2 :(得分:-1)

之所以发生这种情况,是因为当屏幕方向改变时,活动会重新启动。

选项为“不允许活动重新启动。”

对活动的AndroidManifest文件执行此操作。 (就我而言,该活动称为MainActivity。)

//Specify what is done on configuration changes, this line will help you invoke configuration changes in the activity

<activity android:name=".MainActivity" android:configChanges="orientation|keyboardHidden|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

现在调用以下方法;

  @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){

//Only if you want to do something here. If you dont want then leave it blank

            Toast.makeText(this, "Landscape Mode", Toast.LENGTH_SHORT).show();

        }else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

//Only if you want to do something here too.

            Toast.makeText(this, "Portrait Mode", Toast.LENGTH_SHORT).show();
        }
    }

就像魔术一样,它运行完美。我测试过了

尝试一下,看看是否可行。如果它不要求其他替代方法,我会给。