设计模式最佳实践

时间:2016-02-15 17:11:06

标签: java android

所以,我开始用Design Patterns编写应用程序。我被困在中间。请原谅我如果犯了任何愚蠢的错误。

我开始创建像

这样的BaseActivity

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

现在,我的第一个活动SplashActivity将扩展BaseAppCompatActivity,我将其编码如下:

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

问题是我得到白屏,而不是我的布局。为什么? 请以正确和详细的理由提供答案。另外,如何才能进一步改善我的当前BaseAppCompatActivity

编辑:根据请求添加清单:

<?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>

2 个答案:

答案 0 :(得分:2)

问题在于您SplashActivity正在调用onCreate()方法

@Override 
public void onCreate(Bundle savedInstanceState) { 

然而在BaseAppCompatActivity中,您正在使用onCreate()方法和其他参数

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

尝试使用相同的参数。

答案 1 :(得分:0)

是。使用带有一个参数的方法原型是第一步,在BaseAppCompatActivity

中进行了更改
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {

}

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

}

。我还必须在SplashActivity中将onCreate()的方法类型更改为public:

@Override
public void onCreate(Bundle savedInstanceState) {

}

只有这样,才有效。所以,最后我的代码如下所示:

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