只是为了让我清楚,您是说您有几个选项卡,它们使用不同的方向(纵向或横向),并且在切换选项卡和正确设置相应的方向时遇到问题?
回应 Cata 的评论:
是的,所以每当您旋转屏幕时,当前可查看的活动都会被破坏并再次调用 onCreate(如果我记得这些步骤)。您需要做的是调用 getCurrentTab(),它返回代表选项卡的 int 值,并在调用 onCreate 时将其重置为活动选项卡。您可以通过多种方式执行此操作...通过使用处理该问题的小方法并通过 onCreate 调用它,或者使用 onSaveInstanceState(Bundle) 保存当前数据,并使用 onRestoreInstanceState() 重新加载选项卡数据。
您可以设置一个全局 int (int currentTab = 0),而不是在 onCreate() 中设置,在 onSaveInstanceState(Bundle) 方法中,您可以将其保存到当前选项卡 (currentTab = getCurrentTab()),然后在 onRestoreInstanceState()你可以重新设置。
那有意义吗?
请记住,我没有对此进行测试,但如果您不熟悉这两个方法调用,则愿意这样做。
下面是一个将数据保存到 Bundle 的示例 - 还记得 onCreate 接受该活动包作为参数。
@Override
public void onSaveInstanceState(Bundle outState){
// Store UI state to the savedInstanceState.
// This bundle will be passed to onCreate on next call.
super.onSaveInstanceState(outState);
String strMinSec = timer.getText().toString();
String strMs = timerMs.getText().toString();
long curElapstedTime = elapsedTime;
boolean timerStopped = stopped;
int orientation = this.getResources().getConfiguration().orientation;
outState.putString("MinSec", strMinSec);
outState.putString("Ms", strMs);
outState.putLong("Elapsed", elapsedTime);
outState.putBoolean("Stopped", timerStopped);
outState.putInt("Orientation", orientation);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
// Restore UI state from the savedInstanceState.
if (savedInstanceState != null){
String MinSec = savedInstanceState.getString("MinSec");
if (MinSec != null)
{
timer.setText(MinSec);
}
String Ms = savedInstanceState.getString("Ms");
if (Ms != null)
{
timerMs.setText(Ms);
}
long elapsed = savedInstanceState.getLong("Elapsed");
if(elapsed > 0)
elapsedTime = elapsed;
int theOrientation = savedInstanceState.getInt("Orientation");
//if(theOrientation > 0)
//this.setRequestedOrientation(theOrientation);
}
}