android - how to show another activity from splash screen using OOP not thread -


here idea want create component activity in android in way want main activity derived splash activity dervied activity , in splash activity want check license , if available want redirect main activity has derived splash activity.

my whole idea having component extending main activity should automated.

i have done use following code not working smoothly , i'm sure there wrong approach:

//my main activity shown after splash screen public class myactivity extends splashloader { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.setnextactivity(this); super.oncreate(savedinstancestate); setcontentview(r.layout.main); textview tv = (textview) findviewbyid(r.id.tv); tv.settext("hello"); } } //my splash activity public class splashloader extends activity { private static final string error_message = "error message"; private activity nextactivity=null; public activity getnextactivity() { return nextactivity; } public void setnextactivity(activity nextactivity) { this.nextactivity = nextactivity; } /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); linearlayout llayout = new linearlayout(this); llayout.setorientation(linearlayout.vertical); llayout.setlayoutparams(new linearlayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent)); imageview iv = new imageview(this); iv.setlayoutparams(new linearlayout.layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent)); iv.setimageresource(r.drawable.logo); llayout.addview(iv); setcontentview(llayout); boolean isregistered = false; if(!isregistered) { createdialog().show(); } else { // finish(); intent intent = new intent(splashloader.this,getnextactivity().getclass()); startactivity(intent); } } private alertdialog.builder createdialog() { alertdialog.builder builder = new alertdialog.builder(this); dialoginterface.onclicklistener clicklistener = new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialoginterface, int which) { switch (which) { case dialoginterface.button_neutral: system.exit(0); break; default: break; } } } ; builder.setmessage(error_message).setneutralbutton("ok" ,clicklistener ); return builder; } } //and manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example" android:versioncode="1" android:versionname="1.0"> <uses-sdk android:minsdkversion="10"/> <uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.read_phone_state"/> <application android:label="@string/app_name"> <activity android:name="myactivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> </application> 

edit:

there isn't error approach after splash wants load mainactivity has lag or after changing orientation can see mainactivity. behaviour of approach bit peculiar.

in mainactivity.oncreate() calling super.oncreate(). because mainactivity derives splashloader end calling splashloader.oncreate(). in splashloader.oncreate() inflate view , call setcontentview(). in mainactivity.oncreate() again (thereby overwriting work did in splashloader.oncreate()). inflating views takes time (and resources) , may generating part of visible delay. also, splashloader.oncreate() calls startactivity() start mainactivity , done again when mainactivity.oncreate() calls super.oncreate().

if want extending mainactivity splashloader, need fix way lifecycle overrides work. need this:

in splashloader:

@override public void oncreate(bundle savedinstancestate) { setnextactivity(this); super.oncreate(savedinstancestate); // activity-specific stuff dooncreate(savedinstancestate); } // derived classes should not override oncreate(), should // instead override dooncreate() , put activity-specific code there protected void dooncreate(bundle savedinstancestate) { } // splashloader-specific oncreate() stuff linearlayout llayout = new linearlayout(this); llayout.setorientation(linearlayout.vertical); ... etc etc etc ... } 

in mainactivity:

@override protected void dooncreate(bundle savedinstancestate) { // mainactivity-specific oncreate() stuff setcontentview(r.layout.main); textview tv = (textview) findviewbyid(r.id.tv); tv.settext("hello"); } 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -