gpt4 book ai didi

android - 在方向更改android时重新获得当前 Activity 状态

转载 作者:行者123 更新时间:2023-11-29 01:41:01 25 4
gpt4 key购买 nike

我有一个进度条,它从 oncreate 方法开始。当我将屏幕方向更改为横向时,线程重新启动并且旧线程状态消失请给我任何解决这个问题的方法或建议

RelativeLayout layout;
LayoutParams params;
ProgressBar pb;
int i;
Bundle bundle;
Thread t;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// asyn name = new asyn();
// name.execute(10);
bundle = savedInstanceState;
// params=RelativeLayout.
pb = (ProgressBar) findViewById(R.id.progressBar1);
pb.setMax(100);
t = new Thread(ProActivity.this);
t.start();
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
.getDefaultDisplay();
int orientation = display.getOrientation();
switch (orientation) {
case Configuration.ORIENTATION_PORTRAIT:
//
Toast.makeText(ProActivity.this, "portrait", 1000).show();
// pb.setProgress(i);
setContentView(R.layout.landscape);

t = new Thread(ProActivity.this);
t.start();
pb.setProgress(i);

break;
case Configuration.ORIENTATION_LANDSCAPE:
Toast.makeText(ProActivity.this, "Landscape", 1000).show();
break;
}
}

@Override
public void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState = bundle;
}

public void run() {

for (i = 0; i < 100; i++) {

pb.setProgress(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {

e.printStackTrace();
}

}
}

我尝试了这些方法,但无法恢复 Activity 线程状态

问候核糖体

最佳答案

在android list 文件中声明它。

    <activity android:name="yourpackagename.ClassName"     
android:configChanges="keyboardHidden|orientation">
</activity>

关于android - 在方向更改android时重新获得当前 Activity 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24669271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com