So, I am starting writing apps with Design Patterns. And I am stuck in between. Forgive me If i made any silly mistake.
I started with creating a BaseActivity like,
BaseAppCompatActivity:
public abstract class BaseAppCompatActivity extends AppCompatActivity {
private ProgressDialog mProgressDialog;
private Toolbar mToolbar;
private TabLayout mTabLayout;
private boolean hasToolBar;
private boolean hasTabLayout;
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(getLayoutResource());
initiateViews();
initToolBar();
initTabLayout();
}
public abstract int getLayoutResource();
protected abstract void initiateViews();
public void setHasToolBar(boolean hasToolBar){
this.hasToolBar = hasToolBar;
}
public void initToolBar() {
if (hasToolBar){
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
}
}
public Toolbar getToolBar(){
return mToolbar;
}
public void setHasTabLayout(boolean hasTabLayout){
this.hasTabLayout= hasTabLayout;
}
public void initTabLayout(){
if (hasTabLayout){
mTabLayout = (TabLayout)findViewById(R.id.tabLayout);
}
}
public void showProgressDialog() {
mProgressDialog = ProgressDialog.show(this, "Loading", "Please wait...", true, false);
}
public void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
public void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
public void logInformation(Class tag, String description) {
Log.i(tag.getName(), description);
}
public void logError(Class tag, String description) {
Log.e(tag.getName(), description);
}
public void logDebug(Class tag, String description) {
Log.d(tag.getName(), description);
}
public void launchActivity(Class activity, Bundle bundle) {
Intent intent = new Intent(this, activity);
if (bundle != null) {
intent.putExtras(bundle);
}
startActivity(intent);
}
public void launchActivity(Class activity) {
launchActivity(activity, null);
}
public void loadFragment(){}
public String getStringResource(int key){
return getResources().getString(key);
}
}
Now, my first Activity SplashActivity
will be extending that BaseAppCompatActivity
and I coded it as below:
SplashActivity:
public class SplashActivity extends BaseAppCompatActivity {
private Context mContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_splash);
// new Handler().postDelayed(new Runnable() {
// @Override
// public void run() {
// startActivity(new Intent(mContext, IntroductionActivity.class));
// }
// }, 4000);
}
@Override
public int getLayoutResource() {
return R.layout.activity_splash;
}
@Override
protected void initiateViews() {
setHasToolBar(false);
}
}
The problem is that I am getting white screen, and not my layout. Why ?
Please provide answer with proper and detailed reason. Also, how can I improve my current BaseAppCompatActivity
even more ?
Edit: Adding manifest on request:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidveteran.android.xxx">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".baseclass.BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activity.SplashActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activity.IntroductionActivity"
android:label="@string/title_activity_introduction"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".activity.LoginActivity"
android:label="@string/title_activity_login"
android:parentActivityName=".activity.IntroductionActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.androidveteran.android.restclient.activity.IntroductionActivity" />
</activity>
<meta-data
android:name="AA_DB_NAME"
android:value="xxx.db" />
<meta-data
android:name="AA_DB_VERSION"
android:value="1" />
<meta-data
android:name="AA_MODELS"
android:value="com.androidveteran.android.xxx.database.model.User" />
<activity
android:name=".activity.HomeActivity"
android:label="@string/title_activity_home"
android:theme="@style/AppTheme.NoActionBar" />
</application>
</manifest>
activity_splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".activity.SplashActivity">
<include layout="@layout/content_splash" />
</android.support.design.widget.CoordinatorLayout>
content_splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="@color/colorPrimary"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activity.SplashActivity"
tools:showIn="@layout/activity_splash">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/logo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/margin_32"
android:text=""
android:textColor="@android:color/white"
android:textSize="36sp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
BaseAppCompatActivity
. – AlphonsealphonsineBaseAppCompatActivity
have to compulsorily return a layout resource integer. – AlphonsealphonsinesetContentView(R.layout.activity_splash);
in theSplashActivity
? – Oran@Override public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
to@Override public void onCreate(Bundle savedInstanceState) {
in theBaseAppCompatActivity
– OranonCreate
methods have different signatures in the base and splash activity - callingsuper
inonCreate
in the Splash Activtiy calls to AppCompatActivityonCreate
method of the same signature - not the Base ActivityonCreate
as its signature is different - easy to try, put some logging information into theonCreate
in Base Activity, you'll see it never gets called usingsuper
in the Splash Activitiy. – Scathing