java - Display splash screen only when the app is launched and not every time when the activity containing splash screen code, is called -


before describing problem i'd point out aware of other threads asking question, none me have been able solve issue.

the difference between asked questions , question i've splash screen code in main activity , not in activity, have no layout xml file this.

so, problem is, want display splash screen when app started, , done following code when main activity (which contains splash screen code) called activity using intent, splash screen displayed again.

so, first tried use flag variable, initiating 0 when activity created , incrementing 1 when splash screen displayed once. figured out not work because 0 when activity created.

so, i'm trying pass string other activity , trying prevent splash screen again shown in following code:

public class registration extends actionbaractivity {  public final static string extra_message = "com.kaushal.myapplication.message";  databasehelper mydb; edittext username,password; private imageview splashimageview; boolean splashloading = false; int flag=0;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_registration);       string message = null;     intent intent0 = getintent();     message = intent0.getstringextra(registration.extra_message);//string activity.      if(!message.equals("signup has called me")) { //splash screen code in `if`         splashimageview = new imageview(this);         //splashimageview.setbackgroundcolor();         splashimageview.setimageresource(r.drawable.sexy);         setcontentview(splashimageview);         splashloading = false;         handler h = new handler();          h.postdelayed(new runnable() {             public void run() {                 splashloading = false;                 setcontentview(r.layout.activity_registration);                 username = (edittext) findviewbyid(r.id.username);                 password = (edittext) findviewbyid(r.id.password);             }         }, 3000);         flag++;     }      if(flag==0){         username = (edittext) findviewbyid(r.id.username);         password = (edittext) findviewbyid(r.id.password);     }      mydb = new databasehelper(this);     } } 

here registration name of main activity launched when app started.

code of method of activity named sign up passes string:

 public void backtoregistration(view view){     intent intent0 = new intent(this,registration.class);     intent0.putextra(extra_message, "signup has called me");     startactivity(intent0); } 

what problem method? app crashes after launching.

so there number of issues using string in intent flag this. strings aren't setup it. overhead bit more of simple primitive boolean flag.

to way trying

change oncreate code this

super.oncreate(savedinstancestate);  intent intent0 = getintent(); boolean showsplash = getintent().getbooleanextra(registration.flag_key), true);  handlesplashscreen(showsplash); 

and add method, use in recursive way handle showing splash screen.

private void handlesplashscreen(boolean showsplash){     if(showsplash) { //splash screen code in `if`         splashimageview = new imageview(this);         //splashimageview.setbackgroundcolor();         splashimageview.setimageresource(r.drawable.sexy);         setcontentview(splashimageview);         splashloading = false;         handler h = new handler();          h.postdelayed(new runnable() {            public void run() {                handlesplashscreen(false);            }         }, 3000);     }     else{         splashloading = false;         setcontentview(r.layout.activity_registration);         username = (edittext) findviewbyid(r.id.username);         password = (edittext) findviewbyid(r.id.password);     } } 

then, when opening activity activity do

public void backtoregistration(view view){     intent intent0 = new intent(this,registration.class);     intent0.putextra(flag_key, false);     startactivity(intent0); } 

where flag_key private static flag_key = com.kaushal.myapplication.flag; , replaces extra_message string.

how recommend doing it

now, in saying that, isn't @ how recommend doing this. should have splashscreenactivity called on launch , redirects registration mainactivity. get's rid of convolution of having activity 2 different layouts, flags, etc.

public class splashscreenactivity extends activity{      @override     public void oncreate(bundle savedinstancestate){         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_splash);          new handler().postdelayed(new runnable(){             @override             public void run(){                 startactivity(new intent(splashscreenactivity.this, mainactivity.class);                 splashscreenactivity.this.finish();             }         }, 3000);      } } 

where r.layout.activity_splash looks this

<?xml version="1.0" encoding="utf-8"?> <imageview xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:src="r.drawable.sexy"     android:scaletype="centerinside" /> 

then have mainactivity this

public class mainactivity extends actionbaractivity{      // variables here     ...      @override     public void oncreate(bundle savedinstancestate){         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_registration);          username = (edittext) findviewbyid(r.id.username);         password = (edittext) findviewbyid(r.id.password);     }      // other methods     ... } 

lastly, go in manifest , update newly created splashscreenactivity shown on launch , make sure mainactivity still declared appropriately.

<activity     android:name=".splashscreenactivity"     android:label="@string/app_name">      <intent-filter>         <action android:name="android.intent.action.main" />         <category android:name="android.intent.category.launcher" />     </intent-filter> </activity>  <activity     android:name=".mainactivity"/> 

no more flags, no more logic, no more issues. loads splash screen on launch , it. activity finishes after passes intent no longer on stack. well. reason other questions handle things is recommended way of going having splash screen.


Comments