-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBaseActivity.java
254 lines (219 loc) · 8.91 KB
/
BaseActivity.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package com.android.kickstart.activities;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import com.android.kickstart.R;
import com.android.kickstart.dialogs.CustomProgressDialog;
import com.android.kickstart.utils.PreferenceUtil;
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
public static final String TAG = BaseActivity.class.getSimpleName();
/*
* MAX_CLICK_INTERVAL contains Max time interval to prevent double click
* lastClickedTime contains last clicked time of view
*/
public static final long MAX_CLICK_INTERVAL = 1000;
/*
* SharedPreferences
*/
public PreferenceUtil mPreferenceUtil;
protected long lastClickedTime = 0;
/*
* ProgressDialog
*/
private CustomProgressDialog progressDialog;
/*
* abstract method for set view
* @return
*/
public abstract int getActivityView();
/*
* abstract method to initialize components
*/
public abstract void initializeComponents();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getActivityView());
// Initialize preference utility
mPreferenceUtil = new PreferenceUtil(getApplicationContext(), getString(R.string.app_name));
initializeComponents();
}
@Override
public void onClick(View view) {
/*
* Logic for hide keyboard on click
*/
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
/*
* Logic to Prevent the Launch Twice if User makes
* the Tap(Click) very Fast.
*/
if (SystemClock.elapsedRealtime() - lastClickedTime < MAX_CLICK_INTERVAL) {
return;
}
lastClickedTime = SystemClock.elapsedRealtime();
}
/**
* @param className Name of the next Activity (NextActivity.class)
*/
public void startActivity(Class<?> className) {
startActivity(new Intent(getApplicationContext(), className));
}
/**
* @param className Name of the next Activity (NextActivity.class)
* @param resultCode Activity result code
*/
public void startActivity(Class<?> className, int resultCode) {
startActivityForResult(new Intent(getApplicationContext(), className), resultCode);
}
/**
* @param intent Activity Intent
* @param resultCode Activity result code
*/
public void startActivityForResultBack(Intent intent, int resultCode) {
setResult(resultCode, intent);
}
/**
* @param className Name of the next Activity (NextActivity.class)
* @param enterAnim enter animation
* @param exitAnim exit animation
*/
public void startActivity(Class<?> className, int enterAnim, int exitAnim) {
startActivity(new Intent(getApplicationContext(), className));
overridePendingTransition(enterAnim, exitAnim);
}
/**
* @param className Name of the next Activity (NextActivity.class)
* @param resultCode Activity result code
* @param enterAnim enter animation
* @param exitAnim exit animation
*/
public void startActivity(Class<?> className, int resultCode, int enterAnim, int exitAnim) {
startActivityForResult(new Intent(getApplicationContext(), className), resultCode);
overridePendingTransition(enterAnim, exitAnim);
}
/**
* @param intent Activity Intent
* @param resultCode Activity result code
* @param enterAnim enter animation
* @param exitAnim exit animation
*/
public void startActivityForResultBack(Intent intent, int resultCode, int enterAnim, int exitAnim) {
setResult(resultCode, intent);
overridePendingTransition(enterAnim, exitAnim);
}
/*
* Adds the Fragment into layout container
*
* @param fragmentContainerResourceId Resource id of the layout in which Fragment will be added
* @param currentFragment Current loaded Fragment to be hide
* @param nextFragment New Fragment to be loaded into fragmentContainerResourceId
* @param requiredAnimation true if screen transition animation is required
* @param commitAllowingStateLoss true if commitAllowingStateLoss is needed
* @return true if new Fragment added successfully into container, false otherwise
*/
public boolean addFragment(int fragmentContainerResourceId, Fragment currentFragment, Fragment nextFragment, boolean requiredAnimation, boolean commitAllowingStateLoss) {
try {
if (currentFragment == null || nextFragment == null) {
return false;
}
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (requiredAnimation) {
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_NONE);
}
fragmentTransaction.add(fragmentContainerResourceId, nextFragment, nextFragment.getClass().getSimpleName());
fragmentTransaction.addToBackStack(nextFragment.getClass().getSimpleName());
Fragment parentFragment;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
parentFragment = currentFragment.getParentFragment();
if (parentFragment == null) {
fragmentTransaction.hide(currentFragment);
} else {
fragmentTransaction.hide(parentFragment);
}
}
if (!commitAllowingStateLoss) {
fragmentTransaction.commit();
} else {
fragmentTransaction.commitAllowingStateLoss();
}
return true;
} catch (IllegalStateException e) {
e.printStackTrace();
}
return false;
}
/*
* Replaces the Fragment into layout container
*
* @param fragmentContainerResourceId Resource id of the layout in which Fragment will be added
* @param fragmentManager fragmentManager of current activity
* @param nextFragment New Fragment to be loaded into fragmentContainerResourceId
* @param requiredAnimation true if screen transition animation is required
* @param commitAllowingStateLoss true if commitAllowingStateLoss is needed
* @return true if new Fragment added successfully into container, false otherwise
*/
public boolean replaceFragment(int fragmentContainerResourceId, FragmentManager fragmentManager, Fragment nextFragment, boolean requiredAnimation, boolean commitAllowingStateLoss) {
try {
if (nextFragment == null || fragmentManager == null) {
return false;
}
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (requiredAnimation) {
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_NONE);
}
fragmentTransaction.replace(fragmentContainerResourceId, nextFragment, nextFragment.getClass().getSimpleName());
if (!commitAllowingStateLoss) {
fragmentTransaction.commit();
} else {
fragmentTransaction.commitAllowingStateLoss();
}
return true;
} catch (IllegalStateException e) {
e.printStackTrace();
}
return false;
}
/**
* @param message ProgressDialog message
* @param isCancelable true if user need to cancel dialog on touch else false
*/
private void displayDialog(String message, boolean isCancelable) {
progressDialog = new CustomProgressDialog(this, message, isCancelable);
if (!isFinishing()) {
progressDialog.show();
}
}
/**
*
*/
private void dismissDialog() {
if (progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
}
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (progressDialog != null && progressDialog.isShowing())
dismissDialog();
}
}