在方向改变android时重新获得当前的活动状态

时间:2014-07-10 05:47:28

标签: android screen-orientation android-orientation

我有一个进度条,它从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();
        }

    }
}

我尝试了这些方法,但它无法重新获得活动线程状态

此致 ribin

1 个答案:

答案 0 :(得分:1)

在android清单文件中声明它。

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