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(); } 
  • one
    You cannot use this.finish () inside onItemClick, because inside this method this does not refer to context. Remove this and just call finish () - Yuriy SPb
  • one
    No, this does not matter here. You just need to add a flag, as written in the answer below. - Mentat
  • one
    In the comments I did not answer the whole question, but to its part about this.finish only. And this in your code refers to the AdapterView.OnItemClickListener object, and not to the activation. because of the mistake. - YurySPb
  • one
    @YuriySPb As far as I can see, the сloseActivity() method is called from the adapter class, but it is in activation and this in this case refers to the Activity class - pavlofff
  • one
    @pavlofff. As far as I understood the author, he tried to call this.finish() in the body of the onItemClick() method, which is an implementation of the interface method implemented in an anonymous class, passed as a parameter to the setOnItemClickListener() method. It didn’t work for him and he created the closeActivity() method in closeActivity() and could use this.finish() , since at . closeActivity() this is a link to the activity, and in onItemClick() this is a link to an anonymous class that implements the interface. AdapterView.OnItemClickListener . - Yuriy SPb

4 answers 4

According to tyk and documentation :

  1. In the manifest, in the declaration of your MainActivity add

    android: noHistory = "true"

  2. Now leaving this activation (in any way, including launching a new one), she herself calls her finish () and removes from the activation stack

    In principle, yes, Activity so arranged that by default it falls asleep when the other starts. But you can add a flag to the Intent to change this:

     Intent i = new Intent(SelectDictionary.this, MainActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(i); closeActivity(); 
    • one
      What is a flag? :) - user189127
    • one
      Well, type some parameter "yes / no" to configure. - Mentat
    • Does not work .. :( - user189127
     Intent intent = new Intent(ThisActivity.this, NewActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(ThisActivity.this, intent); 
    • You are a bit late:) ... - user189127
    • one
      @ bukashka101 The fact is that the answers are given not to you personally, but to everyone who has a similar problem and the more answers, the better - pavlofff

    at the end of the handler write this command and all

     super.finish();