-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseActivity.java
More file actions
80 lines (69 loc) · 2.31 KB
/
Copy pathBaseActivity.java
File metadata and controls
80 lines (69 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.miaoyi.work.activity;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.widget.TextView;
import com.miaoyi.work.fragment.BaseFragment;
public abstract class BaseActivity extends AppCompatActivity {
/**
* get Layout View ID
*/
protected abstract int getContentViewId();
/**
* get Fragment Content Id
*/
protected abstract int getFragmentContentId();
protected abstract TextView getTopView();
/**
* Add Fragment
* We replace the content fragment you want by getFragmentContentId()
*
* @param fragment
*/
public void addFragment(BaseFragment fragment, String title) {
if (null != fragment) {
if (getSupportFragmentManager().findFragmentByTag(fragment.getClass().getSimpleName()) != null) {
return;
}
if (null != getTopView()) {
getTopView().setText(title);
}
getSupportFragmentManager().beginTransaction().replace(getFragmentContentId(), fragment, title).addToBackStack(fragment.getClass().getSimpleName()).commitAllowingStateLoss();
}
}
/**
* remove Fragment
* we check the back stack entry count first , and
* if >1 , then we use fragment manger pop back,
* else we could just finish this Activity
*/
public void removeFragment() {
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
requestTitle();
} else {
finish();
}
}
/**
* invalidate title
*/
private void requestTitle() {
int j = getSupportFragmentManager().getBackStackEntryCount();
--j;
getTopView().setText(getSupportFragmentManager().getFragments().get(j - 1).getTag());
}
/**
* check app back
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.KEYCODE_BACK == keyCode) {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
finish();
return true;
}
requestTitle();
}
return super.onKeyDown(keyCode, event);
}
}