Differences between the back button and the navigation bar button

In Android programming, there is a difference between the hardware return button and the back button on the navigation bar. My application has a view where, if I click the back button, the application will shut down. But, if I click the button on the navigation bar, it works great. There are various methods called based on two different types of buttons (e.g. oncreate vs onresume).

The class I call the back button:

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 

Besides the error, although this is another problem, why this happens only with the hardware, and not with the back panel in the navigation panel.

+3
source share
2 answers

When you click the back to hardware button or the provided soft back button, it calls the onBackPressed () method in action, you can override this behavior as you like.

The difference from it to the Back Button navigation bar is that you have to deal with it yourself, as you do with the onOptionsItemSelected () method on 'android.R.id.home'.

The problem that you are facing is probably due to the fact that the "previous" activity does not use the "Cursor" correctly. When you click on "ViewContact", it will complete the operation "ViewContact", and onResume () of the previous action will be called again. Therefore, when this happens, it is obvious that your previous activity is trying to reuse the closed cursor, then an error occurs. On your NavUtils.navigateUpFromSameTask (this) you will probably prevent this. BUT OFFICIAL and DEFAULT - this is the one I described.

+4
source

Another important difference. If you go back using the navigation bar button in the action that is the parent of your action, you will call the onNewIntent method. Unlike the hardware button .

0
source

Source: https://habr.com/ru/post/1208300/


All Articles