Design pattern best practices
Asked Answered
A

2

2

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 BaseAppCompatActivityeven 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>
Alphonsealphonsine answered 15/2, 2016 at 17:11 Comment(15)
Please show the android manifest.Arteriole
Please share activity_splash.xml since that has your layout for the screen.Commissure
My Manifest and Layout code is alright, I am pretty sure with it. I going wrong somewhere in implementing my own BaseAppCompatActivity.Alphonsealphonsine
@bigdestroyer You can check my manifest.Alphonsealphonsine
@DougStevenson You can check my layout file.Alphonsealphonsine
Your abstract method isn't implemented in your base class, however you are calling it : setContentView(getLayoutResource());Scathing
@MarkKeen I have kept it abstract purposely so that every activity extending my BaseAppCompatActivity have to compulsorily return a layout resource integer.Alphonsealphonsine
Does it work if uncomment setContentView(R.layout.activity_splash); in the SplashActivity?Oran
@Vasily Kabunov Yes, it works if i uncomment it.. But I didn't want it that way.Alphonsealphonsine
Have you tried showing a very simple layout first just to make sure it works, then slowly add more things to it?Commissure
@Doug Stevenson Yes sir. I tried the same way as you said.Alphonsealphonsine
What design pattern are you trying to apply here?Timbered
Try to change @Override public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { to @Override public void onCreate(Bundle savedInstanceState) { in the BaseAppCompatActivity Oran
@Vasily Kabunov Agreed, the overloaded onCreate methods have different signatures in the base and splash activity - calling super in onCreate in the Splash Activtiy calls to AppCompatActivity onCreate method of the same signature - not the Base Activity onCreate as its signature is different - easy to try, put some logging information into the onCreate in Base Activity, you'll see it never gets called using super in the Splash Activitiy.Scathing
@Kevin Krumwiede I think he's using this are more of a framework, than a pattern.Scathing
O
2

The thing is in your SplashActivity you are calling onCreate() method

@Override 
public void onCreate(Bundle savedInstanceState) { 

however in BaseAppCompatActivity you are using onCreate() method with another parameters

@Override 
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) { 

Try to use the same parameters.

Oran answered 16/2, 2016 at 4:59 Comment(0)
A
1

Yes. Using the method prototype with one argument was the first move, changed in BaseAppCompatActivity

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {

}

to

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

}

. I also had to change the method type of onCreate() to public in SplashActivity:

@Override
public void onCreate(Bundle savedInstanceState) {

}

Only then, it worked. So, finally my code looks like below:

BaseAppCompatActivity:

public abstract class BaseAppCompatActivity extends AppCompatActivity {

    private ProgressDialog mProgressDialog;
    private Toolbar mToolbar;
    private TabLayout mTabLayout;

    private boolean hasToolBar;
    private boolean hasTabLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResourceId());
    }

    protected abstract int getLayoutResourceId();

    protected abstract void initiateViews();

    private void initDatabase() {
        ActiveAndroid.initialize(this);
    }

    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);
    }
}

SplashActivity:

public class SplashActivity extends BaseAppCompatActivity {

    private Context mContext = this;

    @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(mContext, IntroductionActivity.class));
//            }
//        }, 4000);
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initiateViews() {
        setHasToolBar(false);
    }
}
Alphonsealphonsine answered 16/2, 2016 at 3:19 Comment(2)
Good. Will you accept my comment if I rewrite it as an answer?Oran
@VasilyKabunov Why not.. Add your answer with some explanation and I will accept. Not an issue :)Alphonsealphonsine

© 2022 - 2024 — McMap. All rights reserved.