Skip to content

Commit

Permalink
add BaseFragment field 'mActivity' and BaseApp private constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
starry90 committed Mar 10, 2018
1 parent c7c5450 commit 74acb3e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/starry/douban/base/BaseApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class BaseApp {

private ActivityCallback lifeCallback;

private BaseApp() {
}

public static BaseApp getInstance() {
return Holder.INSTANCE;
}
Expand Down
24 changes: 14 additions & 10 deletions app/src/main/java/com/starry/douban/base/BaseFragment.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.starry.douban.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
Expand All @@ -26,6 +28,12 @@ public abstract class BaseFragment extends Fragment implements IBaseActivity, IL

private boolean isSuccess;

/**
* 使用mActivity代替getActivity(),保证Fragment即使在onDetach后,仍持有Activity的引用<p>
* 有引起内存泄露的风险,但相比空指针应用闪退,这种做法更“安全”
*/
protected Activity mActivity;

/**
* 网络请求各种状态显示容器
* <p>Required view 'view_loading_container' with ID 2131427348 for field 'mLoadingDataLayout' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.
Expand All @@ -34,6 +42,12 @@ public abstract class BaseFragment extends Fragment implements IBaseActivity, IL
@BindView(R.id.view_loading_container)
protected LoadingDataLayout mLoadingDataLayout;

@Override
public void onAttach(Context context) {
super.onAttach(context);
mActivity = (Activity) context;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -111,14 +125,4 @@ public void onLoadingComplete() {

}

/**
* 退出Activity
*/
public void finish() {
if (getActivity() != null) {
getActivity().finish();
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


/**
* 懒加载的Framgent
* 懒加载的Fragment
* Fragment基类
*/
public abstract class BaseLazyFragment extends BaseFragment implements IBaseActivity {
Expand Down

0 comments on commit 74acb3e

Please sign in to comment.