daily pastebin goal
44%
SHARE
TWEET

Untitled

a guest Feb 19th, 2019 106 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Override
  2. public void onConfigurationChanged(Configuration newConfig) {
  3.     super.onConfigurationChanged(newConfig);
  4.     mEditTextData = mEditText.getText().tostring();//mEditTextData is a String
  5.                                                    //member variable
  6.     setContentView(R.layout.myLayout);
  7.     initializeViews();
  8. }
  9.  
  10. private void initializeViews(){
  11.     mEditText = (EditText)findViewById(R.id.edittext1);
  12.     mEdiText.setText(mEditTextData);
  13. }
  14.    
  15. @Override
  16. protected void onCreate (Bundle savedInstanceState)
  17. {
  18.     super.onCreate(savedInstanceState);
  19.     setContentView(R.layout.login_screen);
  20.     ...
  21.     ...
  22.     String userName, password;
  23.     if(savedInstanceState!=null)
  24.     {
  25.         userName = savedInstanceState.getString("user_name");
  26.         password= savedInstanceState.getString("password");
  27.     }
  28.  
  29.     if(userName != null)
  30.         userNameEdtTxt.setText(userName);
  31.     if(password != null)
  32.         passEdtTxt.setText(password);
  33. }
  34.    
  35. @Override
  36.     protected void onSaveInstanceState (Bundle outState)
  37.     {
  38.         outState.putString("user_name", userNameEdtTxt.getText().toString());
  39.         outState.putString("password",  passEdtTxt.getText().toString());
  40.     }
  41.    
  42. public class MyActivity extends Activity {
  43.     private static final String TAG = "com.example.handledataorientationchange.MainActivity";
  44.     private static ProgressDialog progressDialog;
  45.     private static Thread thread;
  46.     private static boolean isTaskRunnig;
  47.  
  48.     @Override
  49.     public void onCreate(Bundle savedInstanceState) {
  50.         super.onCreate(savedInstanceState);
  51.         Log.d(TAG, "onCreate");
  52.         setContentView(R.layout.main);
  53.         Button button = (Button) findViewById(R.id.button);
  54.         button.setOnClickListener(new EditText.OnClickListener() {
  55.  
  56.             @Override
  57.             public void onClick(View v) {
  58.                 perform();
  59.                 isTaskRunnig = true;
  60.             }
  61.         });
  62.     }
  63.  
  64.     @Override
  65.     public boolean onCreateOptionsMenu(Menu menu) {
  66.         getMenuInflater().inflate(R.menu.main, menu);
  67.         return true;
  68.     }
  69.  
  70.     public void perform() {
  71.         Log.d(TAG, "perform");
  72.         progressDialog = android.app.ProgressDialog.show(this, null,
  73.                 "Working, please wait...");
  74.         progressDialog
  75.                 .setOnDismissListener(new DialogInterface.OnDismissListener() {
  76.  
  77.                     @Override
  78.                     public void onDismiss(DialogInterface dialog) {
  79.                         //isTaskRunnig = false;
  80.                     }
  81.                 });
  82.         thread = new Thread() {
  83.             public void run() {
  84.                 Log.d(TAG, "run");
  85.                 int result = 0;
  86.                 try {
  87.  
  88.                     // Thread.sleep(5000);
  89.                     for (int i = 0; i < 20000000; i++) {
  90.  
  91.                     }
  92.                     result = 1;
  93.                     isTaskRunnig = false;
  94.                 } catch (Exception e) {
  95.                     e.printStackTrace();
  96.                     result = 0;
  97.                 }
  98.                 Message msg = new Message();
  99.                 msg.what = result;
  100.                 handler.sendMessage(msg);
  101.             };
  102.         };
  103.         thread.start();
  104.     }
  105.  
  106.     // handler to update the progress dialgo while the background task is in
  107.     // progress
  108.     private static Handler handler = new Handler() {
  109.  
  110.         public void handleMessage(Message msg) {
  111.             Log.d(TAG, "handleMessage");
  112.             int result = msg.what;
  113.             if (result == 1) {// if the task is completed successfully
  114.                 Log.d(TAG, "Task complete");
  115.                 try {
  116.                     progressDialog.dismiss();
  117.                 } catch (Exception e) {
  118.                     e.printStackTrace();
  119.                     isTaskRunnig = true;
  120.                 }
  121.  
  122.             }
  123.  
  124.         }
  125.     };
  126.  
  127.     @Override
  128.     protected void onRestoreInstanceState(Bundle savedInstanceState) {
  129.         super.onRestoreInstanceState(savedInstanceState);
  130.         Log.d(TAG, "onRestoreInstanceState" + isTaskRunnig);
  131.         if (isTaskRunnig) {
  132.             perform();
  133.  
  134.         }
  135.     }
  136.  
  137.     @Override
  138.     protected void onSaveInstanceState(Bundle outState) {
  139.         super.onSaveInstanceState(outState);
  140.         Log.d(TAG, "onSaveInstanceState");
  141.         if (thread.isAlive()) {
  142.             thread.interrupt();
  143.             Log.d(TAG, thread.isAlive() + "");
  144.             progressDialog.dismiss();
  145.         }
  146.  
  147.     }
  148.    
  149. android:configChanges="orientation|keyboardHidden">
  150.    
  151. android:configChanges="orientation|keyboardHidden|screenSize">
  152.    
  153. @Override
  154. public void onSaveInstanceState (Bundle outState)
  155. {
  156.      outState.putString("editTextData1", editText1.getText().toString());
  157.      outState.putString("editTextData2", editText2.getText().toString());
  158.  
  159.      super.onSaveInstanceState(outState);
  160. }
  161.  
  162. @Override
  163. public void onCreate(Bundle savedInstanceState)
  164. {
  165.       super.onCreate();
  166.  
  167.       ... find references to editText1, editText2
  168.  
  169.       if (savedInstanceState != null)
  170.       {
  171.            editText1.setText(savedInstanceState.getString("editTextData1");
  172.            editText2.setText(savedInstanceState.getString("editTextData2");
  173.       }
  174. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top