Search code examples
androidscreen-orientationandroid-orientation

regain the current activity state at orientation change android


i have a progress bar it starts at oncreate method. when i changes the screen orientation to landscape the thread is restarted and the old thread status is gone please give me any methods or suggestions for this problem

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();
        }

    }
}

i tried these methods but it cant regain the activity thread status

Regards ribin


Solution

  • Declare it in android manifest file.

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