Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- mEditTextData = mEditText.getText().tostring();//mEditTextData is a String
- //member variable
- setContentView(R.layout.myLayout);
- initializeViews();
- }
- private void initializeViews(){
- mEditText = (EditText)findViewById(R.id.edittext1);
- mEdiText.setText(mEditTextData);
- }
- @Override
- protected void onCreate (Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login_screen);
- ...
- ...
- String userName, password;
- if(savedInstanceState!=null)
- {
- userName = savedInstanceState.getString("user_name");
- password= savedInstanceState.getString("password");
- }
- if(userName != null)
- userNameEdtTxt.setText(userName);
- if(password != null)
- passEdtTxt.setText(password);
- }
- @Override
- protected void onSaveInstanceState (Bundle outState)
- {
- outState.putString("user_name", userNameEdtTxt.getText().toString());
- outState.putString("password", passEdtTxt.getText().toString());
- }
- public class MyActivity extends Activity {
- private static final String TAG = "com.example.handledataorientationchange.MainActivity";
- private static ProgressDialog progressDialog;
- private static Thread thread;
- private static boolean isTaskRunnig;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Log.d(TAG, "onCreate");
- setContentView(R.layout.main);
- Button button = (Button) findViewById(R.id.button);
- button.setOnClickListener(new EditText.OnClickListener() {
- @Override
- public void onClick(View v) {
- perform();
- isTaskRunnig = true;
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- public void perform() {
- Log.d(TAG, "perform");
- progressDialog = android.app.ProgressDialog.show(this, null,
- "Working, please wait...");
- progressDialog
- .setOnDismissListener(new DialogInterface.OnDismissListener() {
- @Override
- public void onDismiss(DialogInterface dialog) {
- //isTaskRunnig = false;
- }
- });
- thread = new Thread() {
- public void run() {
- Log.d(TAG, "run");
- int result = 0;
- try {
- // Thread.sleep(5000);
- for (int i = 0; i < 20000000; i++) {
- }
- result = 1;
- isTaskRunnig = false;
- } catch (Exception e) {
- e.printStackTrace();
- result = 0;
- }
- Message msg = new Message();
- msg.what = result;
- handler.sendMessage(msg);
- };
- };
- thread.start();
- }
- // handler to update the progress dialgo while the background task is in
- // progress
- private static Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- Log.d(TAG, "handleMessage");
- int result = msg.what;
- if (result == 1) {// if the task is completed successfully
- Log.d(TAG, "Task complete");
- try {
- progressDialog.dismiss();
- } catch (Exception e) {
- e.printStackTrace();
- isTaskRunnig = true;
- }
- }
- }
- };
- @Override
- protected void onRestoreInstanceState(Bundle savedInstanceState) {
- super.onRestoreInstanceState(savedInstanceState);
- Log.d(TAG, "onRestoreInstanceState" + isTaskRunnig);
- if (isTaskRunnig) {
- perform();
- }
- }
- @Override
- protected void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- Log.d(TAG, "onSaveInstanceState");
- if (thread.isAlive()) {
- thread.interrupt();
- Log.d(TAG, thread.isAlive() + "");
- progressDialog.dismiss();
- }
- }
- android:configChanges="orientation|keyboardHidden">
- android:configChanges="orientation|keyboardHidden|screenSize">
- @Override
- public void onSaveInstanceState (Bundle outState)
- {
- outState.putString("editTextData1", editText1.getText().toString());
- outState.putString("editTextData2", editText2.getText().toString());
- super.onSaveInstanceState(outState);
- }
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate();
- ... find references to editText1, editText2
- if (savedInstanceState != null)
- {
- editText1.setText(savedInstanceState.getString("editTextData1");
- editText2.setText(savedInstanceState.getString("editTextData2");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement