Destroys an activity.

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.