Destroys an activity.
It is easy to destroy an activity. Just click the return key (click it on your phone ). However, if you want to specify it in the program, it is actually very simple. Use the finish () method. In combination with the previous example, you only need to add the following code to the btn event:
Finish (); // destroy a thread
1 package cn. hengzhe. tishi; 2 3 import android. support. v7.app. appCompatActivity; 4 import android. OS. bundle; 5 import android. view. menu; 6 import android. view. menuItem; 7 import android. view. view; 8 import android. widget. button; 9 import android. widget. toast; 10 11 public class MainActivity extends AppCompatActivity {12 13 @ Override14 protected void onCreate (Bundle savedInstanceState) {15 super. onCreate (savedInstanceState); 16 setContentView (R. layout. activity_main); 17 Button btn = (Button) findViewById (R. id. btn_on); 18 btn. setOnClickListener (new View. onClickListener () {19 @ Override20 public void onClick (View v) {21 // Toast. makeText (MainActivity. this, "milk tea sister, one hundred times, one hundred times", Toast. LENGTH_SHORT ). show (); 22 finish (); // destroy a thread 23} 24 }); 25 26 27} 28 29/* @ onCreateOptionsMenu30 **/31 public boolean onCreateOptionsMenu (Menu menu) {32 getMenuInflater (). inflate (R. menu. menu, menu); 33 return true; 34} 35 36 public boolean onOptionsItemSelected (MenuItem item) {37 switch (item. getItemId () {38 case R. id. add_item: // ID39 Toast defined in the menu file. makeText (this, "Pop Up A menu", Toast. LENGTH_SHORT ). show (); 40 break; 41 case R. id. move_item: // same as above 42 Toast. makeText (this, "close a menu", Toast. LENGTH_SHORT ). show (); 43 default: 44 45 break; 46} 47 return true; 48} 49 50}
All generations are as follows: