Android implementation return key function _android

Source: Internet
Author: User

Record the operation history of user clicks, use stack data structure, frequent Operation Stack top (add, get, delete), use LinkedList

Capture user's return key operation, response return key, return to previous interface

Mainactivity.java

/**
* Return key processing * *
@Override public
boolean onKeyDown (int keycode, keyevent event) {
if (keycode== Keyevent.keycode_back) {
boolean result=middlemanager.getinstance (). GoBack ();
if (!result) {
toast.maketext (mainactivity.this, "Exit Application", 1). Show ();
return false;
}
Return Super.onkeydown (KeyCode, event);
}

Middlemanager.java

Operation History Stack Private linkedlist<string> history=new linkedlist<string> (); /** * Processing Return key * @return/public boolean goBack () {//If empty throws an exception if (History.size () >0) {if (History.size () ==1) {returns Fals
E
} history.removefirst ();
if (History.size () >0) {String key=history.getfirst ();
Baseview Targetview=viewcache.get (key);
Maincontainer.removeallviews ();
View Child=targetview.getview ();
Maincontainer.addview (child);
Child.startanimation (Animationutils.loadanimation (Maincontainer.getcontext (), r.anim.left_to_right));
Currentview=targetview;
return true;
return false; /** * Loading interface/public void Loadview (class<? extends baseview> Baseviewclass) {Baseview baseview=null;//To determine if STR exists
ing Key=baseviewclass.getsimplename (); if (Viewcache.containskey (key)) {Baseview=viewcache.get (key);} else{try {constructor<? extends baseview> constructor; constructor = Baseviewclass.getconstructor (Context.class
);
Baseview=constructor.newinstance (Maincontainer.getcontext ()); catch (excEption e) {e.printstacktrace ();}
Viewcache.put (key, Baseview);
} maincontainer.removeallviews ();
View Child=baseview.getview ();
Maincontainer.addview (child);
Child.startanimation (Animationutils.loadanimation (Maincontainer.getcontext (), r.anim.left_to_right));
Currentview=baseview;
Put the stack history.addfirst (key); }

The above is a small set to introduce the implementation of the Android return key function, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.