When calling the onItemClick method, onItemClick need to close the current Activity .
In closeActivity() through this.finish() for some reason, the Activity cannot be closed.
For this, I created another method in which I try to close it. But as a result, the Activity goes to sleep mode and remains on the Activity stack.
Where is the mistake?
Code of methods (immediately associated with the listener to the ListView ):
lvMain.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(SelectDictionary.this, MainActivity.class); startActivity(i); closeActivity(); } }); private void closeActivity() { this.finish(); }
сloseActivity()method is called from the adapter class, but it is in activation andthisin this case refers to theActivityclass - pavlofffthis.finish()in the body of theonItemClick()method, which is an implementation of the interface method implemented in an anonymous class, passed as a parameter to thesetOnItemClickListener()method. It didn’t work for him and he created thecloseActivity()method incloseActivity()and could usethis.finish(), since at .closeActivity()this is a link to the activity, and inonItemClick()this is a link to an anonymous class that implements the interface.AdapterView.OnItemClickListener. - Yuriy SPb ♦