Pointing to wrong UI from posted Runnable after screen orintation changes
<p>This is my little test program. My problem is that from <strong>run()</strong> method I access to fields of wrong (old) Activity, which was destroyed after screen orientation changed. What's the way to handle this situation?
And, by the way, I must have my activity been recreated, because in real application I have different layouts for portrait and landscape modes!</p>

<pre><code>public class MainActivity extends Activity {
    private EditText edit;
    private Button button;
    private ProgressDialog progressDialog;
    private boolean isLoginInProgress = false;

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

        edit = (EditText) findViewById(R.id.edit_timer);

        button = (Button) findViewById(R.id.btn_start);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (edit.getText().toString().length() == 0) throw new Exception();
                    long dTime = Long.parseLong(edit.getText().toString());
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            MainActivity.this.isLoginInProgress = false;
                            progressDialog.dismiss();
                        }
                }, dTime);

                    progressDialog.show();
                    isLoginInProgress = true;
                } catch (Exception e) {
                    Toast.makeText(MainActivity.this, "bad time value", Toast.LENGTH_SHORT).show();
                }


            }
        });

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("loading");

        if (savedInstanceState != null) { // activity is restarted
            isLoginInProgress = savedInstanceState.getBoolean("fl_login");
            edit.setText(savedInstanceState.getString("edit"));
        }

        if (isLoginInProgress) { // Show dialog again
            progressDialog.show();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putBoolean("fl_login", isLoginInProgress);
        outState.putString("edit", edit.getText().toString());
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        progressDialog.dismiss();
    }
}
</code></pre>
