Skip to content

Commit

Permalink
优化WebViewActivity#onDestroy;同步eclipse版至studio版
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyLemon committed May 3, 2017
1 parent 3ce35ad commit be1c73c
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 137 deletions.
Binary file modified .DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void onCreate(Bundle savedInstanceState) {
finish();
return;
}

//功能归类分区方法,必须调用<<<<<<<<<<
initView();
initData();
Expand Down Expand Up @@ -116,22 +116,22 @@ public void initData() {

WebSettings webSettings = wvWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

wvWebView.requestFocus();

// 设置setWebChromeClient对象
wvWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
// 设置setWebChromeClient对象
wvWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
tvBaseTitle.setText(StringUtil.getTrimedString(title));
}
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
pbWebView.setProgress(newProgress);
}
});
});

wvWebView.setWebViewClient(new WebViewClient(){
@Override
Expand Down Expand Up @@ -184,7 +184,7 @@ public void onDragBottom(boolean rightToLeft) {
wvWebView.goForward();
}
return;
}
}
onBackPressed();
}

Expand Down Expand Up @@ -233,12 +233,11 @@ protected void onResume() {

@Override
protected void onDestroy() {
super.onDestroy();
if (wvWebView != null) {
wvWebView.destroy();
wvWebView = null;
}
wvWebView = null;
super.onDestroy();
}

protected static final int REQUEST_TO_EDIT_TEXT_WINDOW = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,39 +84,56 @@ protected void setTabSelection(int position) {
}
}

/**
* == true >> 每次点击相应tab都加载,调用getFragment方法重新对点击的tab对应的fragment赋值。
* 如果不希望重载,可以重写selectFragment。
*/
protected boolean needReload = false;
/**
* 当前显示的tab所在位置,对应fragment所在位置
*/
protected int currentPosition = 0;
/**选择并显示fragment
* @param position
*/
public void selectFragment(int position) {
//消耗资源很少,不像Fragment<<<<<<
//tab,资源消耗很小<<<<<<
setTabSelection(position);
selectTab(position);
//消耗资源很少,不像Fragment>>>>>>

//tab,资源消耗很小>>>>>>
if (currentPosition == position) {
if (fragments[position] != null && fragments[position].isVisible()) {
Log.e(TAG, "selectFragment currentPosition == position" +
" >> fragments[position] != null && fragments[position].isVisible()" +
" >> return; ");
return;
if (needReload) {
if (fragments[position] != null && fragments[position].isAdded()) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.remove(fragments[position]).commit();
fragments[position] = null;
}
} else {
if (fragments[position] != null && fragments[position].isVisible()) {
Log.w(TAG, "selectFragment currentPosition == position" +
" >> fragments[position] != null && fragments[position].isVisible()" +
" >> return; ");
return;
}
}
}


if (fragments[position] == null) {
fragments[position] = getFragment(position);
}

// 用全局的fragmentTransaction因为already committed 崩溃
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.hide(fragments[currentPosition]);
//全局的fragmentTransaction因为already committed 崩溃
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.hide(fragments[currentPosition]);
if (fragments[position].isAdded() == false) {
fragmentTransaction.add(getFragmentContainerResId(), fragments[position]);
ft.add(getFragmentContainerResId(), fragments[position]);
}
fragmentTransaction.show(fragments[position]).commit();
ft.show(fragments[position]).commit();

this.currentPosition = position;
};
}


// UI显示区(操作UI,但不存在数据获取或处理代码,也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,31 +98,31 @@ protected final void onCreate(Bundle savedInstanceState, int layoutResID, OnBott
super.setContentView(layoutResID <= 0 ? R.layout.base_tab_activity : layoutResID, listener);
}

// //BaseActivity重写setContentView后这个方法一定会被调用,final有无都会导致崩溃,去掉throw Exception也会导致contentView为null而崩溃
// //防止子类中setContentView <<<<<<<<<<<<<<<<<<<<<<<<
// /**
// * @warn 不支持setContentView,传界面布局请使用onCreate(Bundle savedInstanceState, int layoutResID)等方法
// */
// @Override
// public final void setContentView(int layoutResID) {
// setContentView(null);
// }
// /**
// * @warn 不支持setContentView,传界面布局请使用onCreate(Bundle savedInstanceState, int layoutResID)等方法
// */
// @Override
// public final void setContentView(View view) {
// setContentView(null, null);
// }
// /**
// * @warn 不支持setContentView,传界面布局请使用onCreate(Bundle savedInstanceState, int layoutResID)等方法
// */
// @Override
// public final void setContentView(View view, LayoutParams params) {
// throw new UnsupportedOperationException(TAG + "不支持setContentView" +
// ",传界面布局请使用onCreate(Bundle savedInstanceState, int layoutResID)等方法");
// }
// //防止子类中setContentView >>>>>>>>>>>>>>>>>>>>>>>>>
// //BaseActivity重写setContentView后这个方法一定会被调用,final有无都会导致崩溃,去掉throw Exception也会导致contentView为null而崩溃
// //防止子类中setContentView <<<<<<<<<<<<<<<<<<<<<<<<
// /**
// * @warn 不支持setContentView,传界面布局请使用onCreate(Bundle savedInstanceState, int layoutResID)等方法
// */
// @Override
// public final void setContentView(int layoutResID) {
// setContentView(null);
// }
// /**
// * @warn 不支持setContentView,传界面布局请使用onCreate(Bundle savedInstanceState, int layoutResID)等方法
// */
// @Override
// public final void setContentView(View view) {
// setContentView(null, null);
// }
// /**
// * @warn 不支持setContentView,传界面布局请使用onCreate(Bundle savedInstanceState, int layoutResID)等方法
// */
// @Override
// public final void setContentView(View view, LayoutParams params) {
// throw new UnsupportedOperationException(TAG + "不支持setContentView" +
// ",传界面布局请使用onCreate(Bundle savedInstanceState, int layoutResID)等方法");
// }
// //防止子类中setContentView >>>>>>>>>>>>>>>>>>>>>>>>>



Expand Down Expand Up @@ -171,7 +171,7 @@ public void initView() {// 必须调用
* 当前显示的tab所在位置,对应fragment所在位置
*/
protected int currentPosition = 0;

/**选择下一个tab和fragment
*/
public void selectNext() {
Expand All @@ -183,30 +183,39 @@ public void selectNext() {
public void select(int position) {
topTabView.select(position);
}

/**选择并显示fragment
* @param position
*/
public void selectFragment(int position) {
if (currentPosition == position) {
if (needReload == false && fragments[position] != null && fragments[position].isVisible()) {
Log.w(TAG, "selectFragment currentPosition == position" +
" >> fragments[position] != null && fragments[position].isVisible()" +
" >> return; ");
return;
if (needReload) {
if (fragments[position] != null && fragments[position].isAdded()) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.remove(fragments[position]).commit();
fragments[position] = null;
}
} else {
if (fragments[position] != null && fragments[position].isVisible()) {
Log.w(TAG, "selectFragment currentPosition == position" +
" >> fragments[position] != null && fragments[position].isVisible()" +
" >> return; ");
return;
}
}
}
if (needReload || fragments[position] == null) {

if (fragments[position] == null) {
fragments[position] = getFragment(position);
}

//全局的fragmentTransaction因为already committed 崩溃
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.hide(fragments[currentPosition]);
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.hide(fragments[currentPosition]);
if (fragments[position].isAdded() == false) {
fragmentTransaction.add(R.id.flBaseTabFragmentContainer, fragments[position]);
ft.add(R.id.flBaseTabFragmentContainer, fragments[position]);
}
fragmentTransaction.show(fragments[position]).commit();
ft.show(fragments[position]).commit();

this.currentPosition = position;
}
Expand Down Expand Up @@ -257,7 +266,7 @@ public void initData() {// 必须调用
}
}
//返回按钮>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//前进按钮<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
String forwardName = getForwardName();
if (StringUtil.isNotEmpty(forwardName, true)) {
Expand All @@ -273,16 +282,16 @@ public void initData() {// 必须调用
}
}
//前进按钮>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


//tab<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
topTabView = new TopTabView(context, getResources());
llBaseTabTabContainer.removeAllViews();
llBaseTabTabContainer.addView(topTabView.createView(getLayoutInflater()));
topTabView.setCurrentPosition(currentPosition);
topTabView.bindView(getTabNames());
//tab>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


// fragmentActivity子界面初始化<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Expand Down Expand Up @@ -315,7 +324,7 @@ public void initData() {// 必须调用
@Override
@Nullable
public abstract String getForwardName();

@Nullable
private List<View> topRightButtonList = new ArrayList<View>();
/**添加右上方导航栏按钮
Expand Down Expand Up @@ -422,7 +431,7 @@ public void initEvent() {// 必须调用
}
if (tvBaseTabForward != null) {
tvBaseTabForward.setOnClickListener(new OnClickListener() {//没有id

@Override
public void onClick(View v) {
onForwardClick(v);
Expand Down Expand Up @@ -455,17 +464,17 @@ protected void onDestroy() {
super.onDestroy();
topTabView = null;
fragments = null;

ivBaseTabReturn = null;
tvBaseTabReturn = null;
llBaseTabTopRightButtonContainer = null;
llBaseTabTabContainer = null;

tvBaseTabTitle = null;

currentPosition = 0;
needReload = false;

topRightButtonList = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,33 @@ public void select(int position) {
*/
public void selectFragment(int position) {
if (currentPosition == position) {
if (needReload == false && fragments[position] != null && fragments[position].isVisible()) {
Log.w(TAG, "selectFragment currentPosition == position" +
" >> fragments[position] != null && fragments[position].isVisible()" +
" >> return; ");
return;
if (needReload) {
if (fragments[position] != null && fragments[position].isAdded()) {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.remove(fragments[position]).commit();
fragments[position] = null;
}
} else {
if (fragments[position] != null && fragments[position].isVisible()) {
Log.w(TAG, "selectFragment currentPosition == position" +
" >> fragments[position] != null && fragments[position].isVisible()" +
" >> return; ");
return;
}
}
}
if (needReload || fragments[position] == null) {

if (fragments[position] == null) {
fragments[position] = getFragment(position);
}

//全局的fragmentTransaction因为already committed 崩溃
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.hide(fragments[currentPosition]);
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.hide(fragments[currentPosition]);
if (fragments[position].isAdded() == false) {
fragmentTransaction.add(R.id.flBaseTabFragmentContainer, fragments[position]);
ft.add(R.id.flBaseTabFragmentContainer, fragments[position]);
}
fragmentTransaction.show(fragments[position]).commit();
ft.show(fragments[position]).commit();

this.currentPosition = position;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ protected Exception doInBackground(Void... params) {
try {
result = getResponseJson(client, new Request.Builder()
.addHeader(KEY_TOKEN, getToken(url))
.url(sb.toString()).build());
.url(sb.toString())
.build());
//仅供测试 result = "{\"code\":100,\"data\":{\"id\":1,\"name\":\"TestName\",\"phone\":\"1234567890\"}}";
} catch (Exception e) {
Log.e(TAG, "get AsyncTask.doInBackground try { result = getResponseJson(..." +
Expand Down Expand Up @@ -192,8 +193,10 @@ protected Exception doInBackground(Void... params) {

try {
result = getResponseJson(client, new Request.Builder()
.addHeader(KEY_TOKEN, getToken(url)).url(StringUtil.getNoBlankString(url))
.post(fBuilder.build()).build());
.addHeader(KEY_TOKEN, getToken(url))
.url(StringUtil.getNoBlankString(url))
.post(fBuilder.build())
.build());
//仅供测试 result = "{\"code\":102}";
} catch (Exception e) {
Log.e(TAG, "post AsyncTask.doInBackground try { result = getResponseJson(..." +
Expand Down
Loading

0 comments on commit be1c73c

Please sign in to comment.