Skip to content

Commit 42f6be0

Browse files
committed
1:修改BaseActivity,增加BaseFragment;
1 parent fcdf68e commit 42f6be0

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

lib_common/src/main/java/com/guiying/common/base/BaseActivity.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.view.View;
99

1010
import com.guiying.common.R;
11+
import com.guiying.common.utils.Utils;
1112

1213
/**
1314
* <p>Activity基类 </p>
@@ -66,4 +67,63 @@ public boolean onSupportNavigateUp() {
6667
return true;
6768
}
6869

70+
//添加fragment
71+
protected void addFragment(BaseFragment fragment, @IdRes int frameId) {
72+
Utils.checkNotNull(fragment);
73+
getSupportFragmentManager().beginTransaction()
74+
.add(frameId, fragment, fragment.getClass().getSimpleName())
75+
.addToBackStack(fragment.getClass().getSimpleName())
76+
.commitAllowingStateLoss();
77+
78+
}
79+
80+
//替换fragment
81+
protected void replaceFragment(BaseFragment fragment, @IdRes int frameId) {
82+
Utils.checkNotNull(fragment);
83+
getSupportFragmentManager().beginTransaction()
84+
.replace(frameId, fragment, fragment.getClass().getSimpleName())
85+
.addToBackStack(fragment.getClass().getSimpleName())
86+
.commitAllowingStateLoss();
87+
88+
}
89+
90+
//隐藏fragment
91+
protected void hideFragment(BaseFragment fragment) {
92+
Utils.checkNotNull(fragment);
93+
getSupportFragmentManager().beginTransaction()
94+
.hide(fragment)
95+
.commitAllowingStateLoss();
96+
97+
}
98+
99+
100+
//显示fragment
101+
protected void showFragment(BaseFragment fragment) {
102+
Utils.checkNotNull(fragment);
103+
getSupportFragmentManager().beginTransaction()
104+
.show(fragment)
105+
.commitAllowingStateLoss();
106+
107+
}
108+
109+
110+
protected void removeFragment(BaseFragment fragment) {
111+
Utils.checkNotNull(fragment);
112+
getSupportFragmentManager().beginTransaction()
113+
.remove(fragment)
114+
.commitAllowingStateLoss();
115+
116+
}
117+
118+
119+
//移除fragment
120+
protected void popFragment() {
121+
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
122+
getSupportFragmentManager().popBackStack();
123+
} else {
124+
finish();
125+
}
126+
}
127+
128+
69129
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.guiying.common.base;
2+
3+
import android.content.Context;
4+
import android.support.annotation.IdRes;
5+
import android.support.v4.app.Fragment;
6+
7+
import com.guiying.common.utils.Utils;
8+
9+
public abstract class BaseFragment extends Fragment {
10+
11+
protected BaseActivity mActivity;
12+
13+
14+
//获取宿主Activity
15+
protected BaseActivity getHoldingActivity() {
16+
return mActivity;
17+
}
18+
19+
@Override
20+
public void onAttach(Context context) {
21+
super.onAttach(context);
22+
this.mActivity = (BaseActivity) context;
23+
}
24+
25+
26+
//添加fragment
27+
protected void addFragment(BaseFragment fragment, @IdRes int frameId) {
28+
Utils.checkNotNull(fragment);
29+
getHoldingActivity().addFragment(fragment, frameId);
30+
31+
}
32+
33+
//替换fragment
34+
protected void replaceFragment(BaseFragment fragment, @IdRes int frameId) {
35+
Utils.checkNotNull(fragment);
36+
getHoldingActivity().replaceFragment(fragment, frameId);
37+
}
38+
39+
//隐藏fragment
40+
protected void hideFragment(BaseFragment fragment) {
41+
Utils.checkNotNull(fragment);
42+
getHoldingActivity().hideFragment(fragment);
43+
}
44+
45+
46+
//显示fragment
47+
protected void showFragment(BaseFragment fragment) {
48+
Utils.checkNotNull(fragment);
49+
getHoldingActivity().showFragment(fragment);
50+
}
51+
52+
53+
protected void removeFragment(BaseFragment fragment) {
54+
Utils.checkNotNull(fragment);
55+
getHoldingActivity().removeFragment(fragment);
56+
57+
}
58+
59+
60+
//移除fragment
61+
protected void popFragment() {
62+
getHoldingActivity().popFragment();
63+
}
64+
65+
}

lib_common/src/main/java/com/guiying/common/utils/Utils.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.guiying.common.utils;
22

3+
34
import android.app.Activity;
45
import android.content.Context;
56
import android.content.ContextWrapper;
67
import android.content.pm.ApplicationInfo;
78
import android.content.pm.PackageManager;
89
import android.support.annotation.NonNull;
910
import android.support.annotation.StringRes;
11+
import android.support.v4.app.Fragment;
12+
import android.support.v4.app.FragmentManager;
13+
import android.support.v4.app.FragmentTransaction;
1014
import android.view.View;
1115

1216
/**
@@ -87,4 +91,26 @@ public static boolean isAppDebug() {
8791
}
8892
}
8993

94+
95+
/**
96+
* The {@code fragment} is added to the container view with id {@code frameId}. The operation is
97+
* performed by the {@code fragmentManager}.
98+
*/
99+
public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager,
100+
@NonNull Fragment fragment, int frameId) {
101+
checkNotNull(fragmentManager);
102+
checkNotNull(fragment);
103+
FragmentTransaction transaction = fragmentManager.beginTransaction();
104+
transaction.add(frameId, fragment);
105+
transaction.commit();
106+
}
107+
108+
109+
public static <T> T checkNotNull(T obj) {
110+
if (obj == null) {
111+
throw new NullPointerException();
112+
}
113+
return obj;
114+
}
115+
90116
}

0 commit comments

Comments
 (0)