In Android programming, is there a difference between the hardware back button and the back button in the navigation bar. In my application, there is a view where if I click the hardware back button, the app crashes. But, if I click the button in the navigation bar it works perfectly fine. Are different methods called based on the two different types of buttons (such as oncreate vs onresume).
Class I call the back button from:
public class ViewContact extends ActionBarActivity {
Button btnDelete;
TextView name;
TextView position;
TextView email;
TextView phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_contact);
getActionBar().setDisplayHomeAsUpEnabled(true);
name = (TextView)findViewById(R.id.contactName);
position = (TextView)findViewById(R.id.contactPosition);
email = (TextView)findViewById(R.id.contactEmail);
phone = (TextView)findViewById(R.id.contactPhone);
btnDelete = (Button)findViewById(R.id.deleteContactBtn);
Bundle takeBundledData = getIntent().getExtras();
final String contactName = takeBundledData.getString("clickedName");
String contactPosition = takeBundledData.getString("clickedPosition");
String contactEmail = takeBundledData.getString("clickedEmail");
String contactPhone = takeBundledData.getString("clickedPhone");
name.setText(contactName);
position.setText(contactPosition);
email.setText(contactEmail);
phone.setText(contactPhone);
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MySQLiteHelper androidOpenDbHelper = new MySQLiteHelper(getApplicationContext());
SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();
String[] whereClauseArgument = new String[1];
whereClauseArgument[0] = contactName;
// Only difference between UPDATE and DELETE is
//DELETE does not have ContentValues part
sqliteDatabase.delete(MySQLiteHelper.TABLE_NAME_WIL, MySQLiteHelper.COLUMN_NAME_NAME + "=?", whereClauseArgument);
sqliteDatabase.close();
finishActivity(0);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_contact, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Logcat error:
java.lang.RuntimeException: Unable to resume activity: java.lang.IllegalStateException: trying to requery an already closed cursor android.database.sqlite.SQLiteCursor@43cfb6f0
Aside from the error though which is a different issue, why does it only happen with the hardware back and not the navigation bar back button
When you press the 'hardware back button' or the 'provided soft back button' it will trigger the onBackPressed() method in the Activity. You can override this behavior as you wish.
The difference from it to the Navigation Bar 'Back Button' is that on this one you have to handle yourself, like you're doing on the onOptionsItemSelected() method on the 'android.R.id.home' case.
The problem you're having is probably because the 'previous' activity is misusing a 'Cursor'. When you press back on the 'ViewContact', it will finish the 'ViewContact' activity and the onResume() of the previous activity will be called again. So when this happens, apparently your previous activity is trying to reused a closed cursor, then the error happens. On your NavUtils.navigateUpFromSameTask(this) you are probably preventing this from happening. BUT the OFFICIAL and DEFAULT behavior is the one that i described.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With