Android app's welcome Interface implementation code _android

Source: Internet
Author: User
Tags goto rollback

This article describes in detail how to implement the micro-letter initiation interface in the following figure. The starting interface of this class is characterized by the lifetime of the entire application, which occurs only when the first entry is applied, even after the rollback key is pressed back to the desktop. Use this class to start the application of the interface is also: QQ, QQ music, NetEase Cloud music and microblogging and so on.

Knowledge Points:

Android:nohistory attribute of activity in Androidmanifest.xml, i.e. intent.flag_activity_no_history

Implicit type Intent

Fallback stack (backstack)

See the official document for more information.

Implementation code:

Defined

Splashactivity is the starting interface

Mainactivity Main interface

Androidmanifest.xml

<!--The file is Androidmanifest.xml, the following code is the application activity declaration-->

<!--start interface-->
<activity Android:name= ". Splashactivity "
  android:label=" @string/app_name "
  android:nohistory= true" >
 <intent-filter >
 <action android:name= "Android.intent.action.MAIN"/>
 <category android:name= " Android.intent.category.LAUNCHER "/>
 </intent-filter>
</activity>

<!--main interface-->
<activity android:name= ". Mainactivity "android:label=" @string/app_name ">
</activity>

Notably, in the above activity-xml definition, we set the property of Splashactivity to Nohistory to true, which tells the system to remove the activity from the fallback stack as long as it leaves the activity. In addition, the effect of setting the Intent.flag_activity_no_history identity directly in intent is the same as setting this property to True.

Splashactivity.java

Import ...

Public abstract class Splashactivity extends activity implements Runnable {
 final Handler Mhandler = new Handler (); 
    @Override
 protected void onCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (R.layout.activity_splash);

 Mhandler.postdelayed (this, Watts);

 @Override public
 Void Run () {
 Intent Intent = new Intent (this, mainactivtiy.class);
 StartActivity (intent);
 There is no need to call finish () here because the Nohistory property has been set so that the system takes over the finish operation
 }

Mainactivity.java

Import ...

Public abstract class Mainactivity extends activity {
 ...

 @Override public
 void onbackpressed () {
//method 1:goto the default launcher.    It ' s not recommended.
Intent i = new Intent (intent.action_main);
I.setflags (intent.flag_activity_new_task);
I.addcategory (intent.category_home);
StartActivity (i);

Method 2:goto the default launcher.    It ' s recommended.
Movetasktoback (true);
}
}

In the code above, two methods are provided, the first is to switch to the desktop application (i.e., launcher) by implicit intent, and the second is to switch the task in which the current activity resides to the background, and note that Movetasktoback (Boolean nonroot) nonroot parameter, if nonroot=false, the current activity is required to be the top activity of the stack, otherwise the call will have no effect, if nonroot=true, ignore the nonroot=false condition, So we set the nonroot=true here directly

Here, we can always press the rollback button until we switch to the desktop, when splashactivity has been cleaned up by the system, and mainactivity with the task it is in has switched to the background. The next time we start the application, as long as the mainactivity is not recycled by the system, then we see the mainactivity or back to the desktop before the mainactivity.

Hopefully this article will help you develop friends with Android apps.

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.