Android App boot page implementation-load the first time the app enters

Source: Internet
Author: User

The App Guide page is an essential element for every premium app, so let's look at how to load the boot page only the first time you enter the app.

1. Determine whether to enter the application for the first time:

package com.yayun.guide;  import android.app.activity;import android.content.context; Import android.content.intent;import android.content.sharedpreferences;import android.os.bundle ; import android.os.handler;import android.os.message;import android.widget.toast;  /* * *  function: Use Viewpager to achieve the first entry to the application of the Guide page  *  *  (1) Determine whether it is the first time to load the application-to take the method of reading Sharedpreferences  *  (2) Yes, then enter the boot activity; No, then go to mainactivity *  (3) 5s and perform (2) operation  *  *  @author  yayun * */public class SplashActivity extends Activity {        @Override     protected void oncreate (bundle  savedinstancestate)  {        super.oncreate (savedInstanceState);         //setcontentview (R.layout.activity_splash);        &Nbsp;          boolean mfirst = isfirstenter ( Splashactivity.this,splashactivity.this.getclass (). GetName ());         Toast.maketext (This, mfirst+, toast.length_short). Show ();         if (Mfirst)              Mhandler.sendemptymessagedelayed (switch_guidactivity,100);         else             mhandler.sendemptymessagedelayed ( SWITCH_MAINACTIVITY,100);        sharedpreferences  Sharedpreferences= this.getsharedpreferences (my_pref, mode_private);         sharedpreferences.edit (). putstring (Guide_activity, false). Commit ();                       }              //****************************************************************     //  to determine if the application was first loaded, read the Guide_activity field in Sharedpreferences     //****************     private static final  String sharedpreferences_name = my_pref;    private static final  string key_guide_activity = guide_activity;    private boolean  isfirstenter (Context context,string classname) {         if (context==null | |  classname==null| |. Equalsignorecase (className)) return false;        string  Mresultstr = context.getsharedpreferences (Sharedpreferences_name, contExt. mode_world_readable)                   .getstring (key_guide_activity, );//Get all class names   such as  com.my.MainActivity         if (Mresultstr.equalsignorecase (false))              return false;        else             return true;    }                 //************     // handler: Jump to different page     //     private final static  Int switch_mainactivity = 1000;    private final static int  switch_guidactivity = 1001;    public handler mhandler = new  Handler () {        public void handlemessage (Message msg )  {            switch (msg.what) {             case SWITCH_MAINACTIVITY:                 Intent mIntent =  New intent ();                 mintent.setclass (Splashactivity.this, welcome.class);                 splashactivity.this.startactivity (mIntent);                 splashactivity.this.finish ();                 break;             case SWITCH_GUIDACTIVITY:                 mintent = new intent ();                 mintent.setclass ( Splashactivity.this, guideactivity.class);                 splashactivity.this.startactivity (mintent);                 splashactivity.this.finish ();                 break;             }             super.handleMessage (msg);         }    };} 

The principle is very simple, enter the time to judge whether there is no value, if no value is the first time to enter, then jump into the Application Guide page, otherwise, jump into the main page.

2.ViewPager to achieve page turn effect:

Package com.yayun.guide; import java.util.arraylist;import java.util.list; import  android.app.Activity;import android.content.Intent;import android.os.Bundle;import  android.support.v4.view.pageradapter;import android.support.v4.view.viewpager;import  android.support.v4.view.viewpager.onpagechangelistener;import android.view.layoutinflater;import  Android.view.view;import android.view.view.onclicklistener;import android.view.viewgroup;import  android.view.Window;import android.widget.Button;import android.widget.LinearLayout;  public class guideactivity extends activity implements onclicklistener{      private ViewPager mViewPager;    private  Pageradapter madapter;    private list<view> mviews = new  ArrayList<view> ();    // Tab     private linearlayout mtabweixin;    private  LinearLayout mTabFrd;    private LinearLayout mTabAddress;     private LinearLayout mTabSetting;          private Button mEnterButton;                  @Override     protected void oncreate (bundle  Savedinstancestate)     {        super.oncreate ( Savedinstancestate);         requestwindowfeature (Window.FEATURE_NO_ TITLE);         setcontentview (R.layout.activity_main);          initview ();          Initevents ();    &nbSp; }     private void initevents ()     {                Mviewpager.setonpagechangelistener (New onpagechangelistener ()          {              @Override              public void onpageselected (int arg0 )             {                                }               @Override             public void  Onpagescrolled (int arg0, float arg1,  int arg2)             {              }               @Override              public void onpagescrollstatechanged (int arg0)              {             }         });    }      Private void initview ()     {         mviewpager =  (Viewpager)  findviewbyid (R.id.id_viewpager);               layoutinflater minflater = layoutinflater.from ( this);     &Nbsp;   view tab01 = minflater.inflate (r.layout.tab01, null);         view tab02 = minflater.inflate (R.layout.tab02, null);         view tab03 = minflater.inflate (R.LAYOUT.TAB03,  null);         view tab04 = minflater.inflate ( R.layout.tab04, null);         mviews.add (TAB01);         mviews.add (TAB02);         Mviews.add (TAB03);         mviews.add (TAB04);         menterbutton= (Button) Tab04.findviewbyid (r.id.imgbtn_enter);         menterbutton.setonclicklistener (New onclicklistener ()  {                           @Override              public void onclick (VIEW&NBSP;V)  {                intent  intent=new intent (Guideactivity.this,welcome.class);                 startactivity (Intent);                               }        });          madapter = new pageradapter ()          {              @Override      &nbsP;      public void destroyitem (Viewgroup container, int  position,                     object object)              {                 Container.removeview (Mviews.get (position));             }              @Override              public object instantiateitem (ViewGroup  container, int position)              {                view  view = mviews.get (Position);                 Container.addview (view);                 return view;            }               @Override              public boolean isviewfromobject (VIEW&NBSP;ARG0, &NBSP;OBJECT&NBSP;ARG1)             {                 return arg0 ==  arg1;            }               @Override              public&nbSp;int getcount ()             {                 return mviews.size ();             }         };         mviewpager.setadapter (MAdapter);      }      @Override     public void  OnClick (View v)  {             }   }</view></view>

3. Boot page layout file:

<linearlayout android:layout_height= "match_parent" android:layout_width= "Match_parent" android:orientation= " Vertical "xmlns:android=" http://schemas.android.com/apk/res/android "xmlns:tools=" http://schemas.android.com/ Tools > </android.support.v4.view.viewpager> </linearlayout>

Run the code below:

We can find that the first time you enter the page, the toast prints true, indicating that the application first enters, then loads the boot page.

Quit the app, and once again, toast prints false, indicating that the app is not the first time it's entered. At this point our functions are basically realized.

This article is from the "No Water Fish" blog, please be sure to keep this source http://javaqun.blog.51cto.com/10687700/1735378

Android App boot page implementation-load the first time the app enters

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.