title: Android bugs on device. What should I do to fix them?
body: <p>In my application I noticed these three things:</p>
  
  <p>-The back button is enabled when going from one activity to another enabling the user to click on back to the original activity. The problem is I don't want the user to click on Back at a certain point in my application. I don't want to disable the back button completely in my application, only when one intent is called. How can I do that?</p>
  
  <p>-I noticed something strange... when a toast notification pops up in my application all is well until I exit my application. When I exit my application, some of the toast notifications are residual and are popping outside of my application. Is there a reason for that? Did I miss something in the activity lifecycle to handle the cancellation of toasts at a certain point?</p>
  
  <p>Lastly, this one is rather tough to solve. How do I lock my screen so that when the user rotates the device, that the activity doesn't not get called again and the asynctask can still resume without starting over again?</p>
  
  <p>Thanks a lot for your time. Just curious why these things happen and what should I look into?</p>
  
  <p>Here's my code: </p>
  
  <pre><code>//Main Activity.java
  
  package com.example.Patient_Device;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.content.Context;
  import android.content.Intent;
  import android.database.sqlite.SQLiteDatabase;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  
  import java.io.*;
  
  
  public class MainActivity extends Activity {
  
      //fields
      private ProgressDialog progressBar;
      private Context context;
  
  
      /**
       * Called when the activity is first created.
       */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.start_setup);
          //Set the context
          context = this;
  
          //Initialize the start setup button and add an onClick event listener to the button
          final Button start_setup_button = (Button) findViewById(R.id.start_setup_button);
          start_setup_button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View view) {
  
                  //Executes the AsyncTask
                  new RetrieveInfoTask().execute();
  
                  //Instantiates the intent to launch a new activity
                  Intent myIntent = new Intent(MainActivity.this, RetrieveInfoActivity.class);
                  MainActivity.this.startActivity(myIntent);
  
              }
          });
      }
  
      public class RetrieveInfoTask extends AsyncTask&lt;Void, Void, Void&gt; {
  
          //Called on the UI thread to execute progress bar
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
  
              progressBar = new ProgressDialog(context);
              progressBar.setIndeterminate(true);
              progressBar.setCancelable(false);
              progressBar.setMessage(MainActivity.this.getString(R.string.retrieve_info));
              progressBar.show();
          }
  
          //Methods that retrieves information from the user device. This is performed in the Background thread
          private void retrieveInfo() {
  
              try {
  
                  //Reading the drawable resource line by line
                  String str="";
                  StringBuffer buf = new StringBuffer();
                  InputStream is = MainActivity.this.getResources().openRawResource(R.drawable.user_info);
                  BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                  if (is!=null) {
                      while ((str = reader.readLine()) != null) {
                          buf.append(str + "\n" );
                      }
                  }
                  is.close();
  
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
  
  
          //doInBackground calls retrieveInfo() to perform action in Background
          @Override
          protected Void doInBackground(Void... params) {
  
              retrieveInfo();
              return null;
          }
  
          //When the background task is done, dismiss the progress bar
          @Override
          protected void onPostExecute(Void result) {
              if (progressBar!=null) {
                  progressBar.dismiss();
              }
          }
  
  
      }
  }
  
  //RetrieveInfoActivity.java
  
  package com.example.Patient_Device;
  
  import android.app.Activity;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.content.res.Resources;
  import android.os.BatteryManager;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class RetrieveInfoActivity extends Activity {
  
      private static String TAG = "RetrieveInfoActivity";
      private Context context;
      String fileLastSync = "09-18-2014 03:47 PM";
  
     @Override
     public void onCreate(Bundle savedInstanceState) {
  
              context = this;
  
              super.onCreate(savedInstanceState);
              setContentView(R.layout.retrieve_info);
  
              //Once the new activity is launched, the setup is complete
              Toast.makeText(getApplicationContext(), "Setup Complete!",
                      Toast.LENGTH_LONG).show();
  
              //Gets the 'last synced' string and sets to datetime of the last sync
              Resources resources = context.getResources();
              String syncString = String.format(resources.getString(R.string.last_sync), fileLastSync);
  
              //Dynamically sets the datetime of the last sync string
              TextView lastSyncTextView = ((TextView) findViewById(R.id.last_sync) );
              lastSyncTextView.setText(syncString);
  
              //calls registerReceiver to receive the broadcast for the state of battery
              this.registerReceiver(this.mBatInfoReceiver,new
                      IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  
  
     }
  
      private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver()  {
  
          @Override
          public void onReceive(Context arg0, Intent intent) {
  
              //Battery level
              int level = intent.getIntExtra("level", 0);
  
              //Dynamically sets the value of the battery level
              TextView batteryTextView = ((TextView) findViewById(R.id.battery) );
              batteryTextView.setText("Battery Level: " + String.valueOf(level)+ "%");
  
              //If the battery level drops below 25%, then announce the battery is low
              //TODO: Add 25 to constants file.
              if(level &lt; 25) {
  
                  Toast.makeText(getApplicationContext(), "Low Battery!",
                          Toast.LENGTH_LONG).show();
  
  
              }
              //Plugged in Status
              int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
  
              //Battery Status
              int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  
              //If the device is charging or contains a full status, it's charging
              boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                      status == BatteryManager.BATTERY_STATUS_FULL;
  
              //If the device isCharging and plugged in, then show that the battery is charging
              if(isCharging &amp;&amp; plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB) {
  
                  Toast.makeText(getApplicationContext(), "Charging.." + String.valueOf(level)+ "%",
                          Toast.LENGTH_LONG).show();
  
              }else{
                  Toast.makeText(getApplicationContext(), "Unplugged!",
                          Toast.LENGTH_LONG).show();
              }
  
  
          }
  
      };
  
      @Override
      public void onDestroy() {
  
          try {
              super.onDestroy();
              unregisterReceiver(this.mBatInfoReceiver);
          }
          catch (Exception e) {
              Log.e(RetrieveInfoctivity.TAG, getClass() + " Releasing receivers-" + e.getMessage());
          }
  
      }
  
  }
  
  //StartSetupActivity.java
  
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  
  public class StartSetupActivity extends Activity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
  
      }
  }
  
  //FragmentsActivity.java
  
  
  import android.app.Fragment;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class FragmentsActivity extends Fragment{
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          return inflater.inflate(R.layout.main, container, false);
      }
  }
  </code></pre>