- Overwrite the return key event listener, so that the back key function is similar to the home key, so that acitivty back to the background without being destroyed by the system, the code is as follows:
- public Span style= "margin:0px; padding:0px; Border:none ">  boolean Span style= "margin:0px; padding:0px; Border:none "> onkeydown ( int keycode, keyevent event) {
- Packagemanager pm = Getpackagemanager ();
- ResolveInfo Homeinfo =
- Pm.resolveactivity (new intent (intent.action_main) addcategory (intent.category_home), 0
- if (keycode == Keyevent.keycode_back) {
- activityinfo ai = homeinfo.activityinfo;
- intent startintent = new Span style= "margin:0px; padding:0px; Border:none "> intent (intent.action_main);
- Startintent.addcategory (intent.category_launcher);
- Startintent.setcomponent (new componentname (Ai.packagename, ai.name));
- Startactivitysafely (startintent);
- return   true ;    
- } else     
- return   super .onkeydown (keycode, event);
-     }  
- private   void startactivitysafely (intent intent) {
- Intent.addflags (Intent.flag_activity_new_task);
- Try {
- StartActivity (intent);
- } catch (activitynotfoundexception e) {
- Toast.maketext (this ,  " null " Span style= "margin:0px; padding:0px; Border:none ">,    
- toast.length_short). Show ();
- } catch (securityexception e) {
- Toast.maketext (this ,  " null " Span style= "margin:0px; padding:0px; Border:none ">,    
- Toast.length_short). Show ();
- }
- }
- The article is reproduced from the Internet, thanks to the original author's selfless sharing.
Instructions for Android to run the app in the background