Skip to content

Commit

Permalink
在组件化切换时,优化application启动逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchong211 committed Mar 7, 2019
1 parent ef1b8bd commit 7064bef
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 188 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package com.ns.yc.lifehelper.base.app;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.ycbjie.library.base.InitializeService;
import com.ycbjie.library.arounter.ARouterUtils;
import com.ycbjie.library.base.config.AppConfig;
import com.ycbjie.library.base.LibApplication;
import com.ycbjie.music.base.BaseAppHelper;


Expand All @@ -23,18 +16,7 @@
* GitHub https://github.com/yangchong211
* </pre>
*/
public class BaseApplication extends Application {


/**
* 这个最先执行
*/
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}

public class BaseApplication extends LibApplication {

/**
* 程序启动的时候执行
Expand All @@ -43,60 +25,7 @@ protected void attachBaseContext(Context base) {
public void onCreate() {
Log.d("Application", "onCreate");
super.onCreate();
AppConfig.INSTANCE.initConfig(this);
BaseAppHelper.get().init(this);
//在子线程中初始化
InitializeService.start(this);
}



/**
* 程序终止的时候执行
*/
@Override
public void onTerminate() {
Log.d("Application", "onTerminate");
super.onTerminate();
AppConfig.INSTANCE.closeRealm();
AppConfig.INSTANCE.closeExecutor();
ARouterUtils.destroy();
}


/**
* 低内存的时候执行
*/
@Override
public void onLowMemory() {
Log.d("Application", "onLowMemory");
super.onLowMemory();
Glide.get(this).clearMemory();
}


/**
* HOME键退出应用程序
* 程序在内存清理的时候执行
*/
@Override
public void onTrimMemory(int level) {
Log.d("Application", "onTrimMemory");
super.onTrimMemory(level);
if (level == TRIM_MEMORY_UI_HIDDEN){
Glide.get(this).clearMemory();
}
Glide.get(this).trimMemory(level);
}


/**
* onConfigurationChanged
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
Log.d("Application", "onConfigurationChanged");
super.onConfigurationChanged(newConfig);
}


Expand Down
9 changes: 3 additions & 6 deletions book/src/main/java/com/ycbjie/book/BookApp.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.ycbjie.book;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.blankj.utilcode.util.Utils;
import com.ycbjie.library.base.LibApplication;

/**
* <pre>
Expand All @@ -15,17 +13,16 @@
* revise:
* </pre>
*/
public class BookApp extends Application {
public class BookApp extends LibApplication {

@Override
public void onCreate() {
super.onCreate();
Utils.init(this);
}

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.ycbjie.douban;
package com.ycbjie.douban.app;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.blankj.utilcode.util.Utils;
import com.ycbjie.library.base.LibApplication;

/**
* <pre>
Expand All @@ -15,17 +13,16 @@
* revise:
* </pre>
*/
public class DouBanApp extends Application {
public class DouBanApp extends LibApplication {

@Override
public void onCreate() {
super.onCreate();
Utils.init(this);
}

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import rx.subscriptions.CompositeSubscription;

/**
* ================================================
* 者:杨充
* 本:1.0
* 创建日期:2017/12/1
* 述:知乎评论
* 修订历史:
* ================================================
* <pre>
* @author 杨充
* blog : https://github.com/yangchong211
* time : 2017/8/22
* desc : 豆瓣读书页面
* revise:
* </pre>
*/
public class DouBookPresenter implements DouBookContract.Presenter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ public class DouBookActivity extends BaseActivity implements View.OnClickListene
private ArrayList<String> mTitleList;
private ArrayList<Fragment> mFragments;


@Override
protected void onDestroy() {
super.onDestroy();
}

@Override
public int getContentView() {
return R.layout.base_tab_view;
Expand All @@ -52,8 +46,8 @@ public int getContentView() {
public void initView() {
llTitleMenu = findViewById(R.id.ll_title_menu);
toolbarTitle = findViewById(R.id.toolbar_title);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
vpContent = (ViewPager) findViewById(R.id.vp_content);
tabLayout = findViewById(R.id.tab_layout);
vpContent = findViewById(R.id.vp_content);

initToolBar();
initFragmentList();
Expand All @@ -80,8 +74,6 @@ public void onClick(View view) {
int i = view.getId();
if (i == R.id.ll_title_menu) {
finish();

} else {
}
}

Expand All @@ -103,13 +95,14 @@ private void initFragmentList() {


private void initViewPagerAndTab() {
/**
/*
* 注意使用的是:getChildFragmentManager,
* 这样setOffscreenPageLimit()就可以添加上,保留相邻2个实例,切换时不会卡
* 但会内存溢出,在显示时加载数据
*/
FragmentManager supportFragmentManager = getSupportFragmentManager();
BasePagerAdapter myAdapter = new BasePagerAdapter(supportFragmentManager, mFragments, mTitleList);
BasePagerAdapter myAdapter = new BasePagerAdapter(supportFragmentManager,
mFragments, mTitleList);
vpContent.setAdapter(myAdapter);
// 左右预加载页面的个数
vpContent.setOffscreenPageLimit(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,28 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.Toast;

import com.blankj.utilcode.util.NetworkUtils;
import com.ycbjie.douban.R;
import com.ycbjie.douban.api.DouBookModel;
import com.ycbjie.library.base.mvp.BaseLazyFragment;
import com.ycbjie.douban.bean.DouBookBean;
import com.ycbjie.douban.contract.DouBookContract;
import com.ycbjie.douban.presenter.DouBookPresenter;
import com.ycbjie.douban.view.activity.DouBookActivity;
import com.ycbjie.douban.view.activity.DouBookDetailActivity;
import com.ycbjie.douban.view.adapter.DouBookAdapter;
import com.ycbjie.douban.bean.DouBookBean;
import com.ycbjie.library.base.mvp.BaseLazyFragment;
import com.ycbjie.library.weight.manager.FullyGridLayoutManager;

import org.yczbj.ycrefreshviewlib.YCRefreshView;
import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import rx.functions.Action1;



Expand All @@ -45,8 +40,6 @@
*/
public class DouBookFragment extends BaseLazyFragment implements DouBookContract.View{


private static final String TYPE = "DouBookFragment";
YCRefreshView recyclerView;
private DouBookActivity activity;
private String mType;
Expand All @@ -57,7 +50,7 @@ public class DouBookFragment extends BaseLazyFragment implements DouBookContract
public static DouBookFragment newInstance(String param1) {
DouBookFragment fragment = new DouBookFragment();
Bundle args = new Bundle();
args.putString(TYPE, param1);
args.putString("DouBookFragment", param1);
fragment.setArguments(args);
return fragment;
}
Expand All @@ -79,7 +72,7 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
Bundle arguments = getArguments();
mType = arguments.getString(TYPE);
mType = arguments.getString("DouBookFragment");
}
}

Expand Down Expand Up @@ -108,21 +101,18 @@ public void initView(View view) {

@Override
public void initListener() {
adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
Intent intent = new Intent(activity, DouBookDetailActivity.class);
intent.putExtra("title",adapter.getAllData().get(position).getTitle());
intent.putExtra("author",adapter.getAllData().get(position).getAuthor().get(0));
intent.putExtra("image",adapter.getAllData().get(position).getImage());
intent.putExtra("average",adapter.getAllData().get(position).getRating().getAverage());
intent.putExtra("numRaters",String.valueOf(adapter.getAllData().get(position).getRating().getNumRaters()));
intent.putExtra("pubdate",adapter.getAllData().get(position).getPubdate());
intent.putExtra("publisher",adapter.getAllData().get(position).getPublisher());
intent.putExtra("id",adapter.getAllData().get(position).getId());
intent.putExtra("alt",adapter.getAllData().get(position).getAlt());
startActivity(intent);
}
adapter.setOnItemClickListener(position -> {
Intent intent = new Intent(activity, DouBookDetailActivity.class);
intent.putExtra("title",adapter.getAllData().get(position).getTitle());
intent.putExtra("author",adapter.getAllData().get(position).getAuthor().get(0));
intent.putExtra("image",adapter.getAllData().get(position).getImage());
intent.putExtra("average",adapter.getAllData().get(position).getRating().getAverage());
intent.putExtra("numRaters",String.valueOf(adapter.getAllData().get(position).getRating().getNumRaters()));
intent.putExtra("pubdate",adapter.getAllData().get(position).getPubdate());
intent.putExtra("publisher",adapter.getAllData().get(position).getPublisher());
intent.putExtra("id",adapter.getAllData().get(position).getId());
intent.putExtra("alt",adapter.getAllData().get(position).getAlt());
startActivity(intent);
});
}

Expand Down Expand Up @@ -198,15 +188,12 @@ public void onErrorClick() {
});

//刷新
recyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if (NetworkUtils.isConnected()) {
getTopMovieData(mType , 0 , 30);
} else {
recyclerView.setRefreshing(false);
Toast.makeText(activity, "网络不可用", Toast.LENGTH_SHORT).show();
}
recyclerView.setRefreshListener(() -> {
if (NetworkUtils.isConnected()) {
getTopMovieData(mType , 0 , 30);
} else {
recyclerView.setRefreshing(false);
Toast.makeText(activity, "网络不可用", Toast.LENGTH_SHORT).show();
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion douban/src/main/module/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


<application
android:name=".DouBanApp"
android:name=".app.DouBanApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_douban_name"
Expand Down
28 changes: 28 additions & 0 deletions gank/src/main/java/com/ycbjie/gank/app/GankApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ycbjie.gank.app;

import android.content.Context;

import com.ycbjie.library.base.LibApplication;

/**
* <pre>
* @author 杨充
* blog : https://github.com/yangchong211
* time : 2017/05/23
* desc : 注意,只有从组件library切换到application才会用到
* revise:
* </pre>
*/
public class GankApp extends LibApplication {

@Override
public void onCreate() {
super.onCreate();
}

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
}

}
Loading

0 comments on commit 7064bef

Please sign in to comment.