我有两个活动,第一个加载网址(简单webview),第二个活动从listview获取网址的ID,然后回调第一个加载网址的活动。
我在第一个活动中使用onActivityResult()
方法来获取额外数据(url id)。
我的问题是当我想在方法onActivityResult()
中的第一个Activity中显示ProgressDialog之后,在第二个活动中从ListView中选择项目后,我得到了一个ProgressDialog但它只显示了几秒钟(非常快)然后在完成加载之前消失URL(web视图)
这是我希望帮助我的拖曳活动的代码。
public class LibActivity extends Activity {
private ImageButton btnCategoriesList;
public static final int BOOK_CATEGORY_REQUEST_CODE = 1;
private WebView webView;
public ProgressDialog dialog;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lib);
handler = new Handler();
dialog = ProgressDialog.show(this, "Loading", "Please wait", true, false);
DisplayMetrics displaymetrics = new DisplayMetrics();
webView = (WebView) findViewById(R.id.categoryWebView);
webView.setWebViewClient(new CategoryItemWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://maktabati.mobi/V3/store.php?catid=" + "91");
btnCategoriesList = (ImageButton) findViewById(R.id.btn_category_list);
btnCategoriesList.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showBooksCategories();
}
});
}
/**
*
*/
protected void showBooksCategories() {
Intent intent = new Intent(getApplicationContext(), BooksCategoriesActivity.class);
startActivityForResult(intent, BOOK_CATEGORY_REQUEST_CODE);
}
/**
*
* @author Jamil
*
*/
private class CategoryItemWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("book")) {
String bookId = url.substring(12);
Intent intent = new Intent(getApplicationContext(), BookDetailActivity.class);
intent.putExtra("BOOK_ID", bookId);
startActivity(intent);
view.stopLoading();
}
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
dialog.dismiss();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
if (requestCode == BOOK_CATEGORY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
dialog = ProgressDialog.show(this, "Loading", "Please wait", true);
new Thread() {
public void run() {
webView.loadUrl("http://maktabati.mobi/V3/store.php?catid=" + data.getStringExtra("CATEGORY_ID"));
handler.post(new Runnable() {
@Override
public void run() {
dialog.dismiss();
}
});
}
}.start();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
第二项活动
public class BooksCategoriesActivity extends Activity {
ItemsCategoriesAdapter adapter;
ArrayList<Category> lstCategory;
Handler handlerProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.books_categories_list);
handlerProgressDialog = new Handler();
CategoryHandler handler = new CategoryHandler();
ParserCreater parserCreater = new ParserCreater(handler);
parserCreater.parse("http://maktabati.mobi/V3/getCategories.php");
lstCategory = handler.getLstCategory();
final ListView listView = (ListView) findViewById(R.id.categories_list);
adapter = new ItemsCategoriesAdapter(lstCategory);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int position, long arg3) {
Intent intent = new Intent(getApplicationContext(), LibActivity.class);
intent.putExtra("CATEGORY_ID", lstCategory.get(position).getId());
BooksCategoriesActivity.this.setResult(RESULT_OK, intent);
finish();
}
});
}
由于
答案 0 :(得分:1)
请尝试从onActivityResult函数中删除以下代码时会发生什么:
handler.post(new Runnable() {
@Override
public void run() {
dialog.dismiss();
}
});
应onPageFinished()
之后调用webView.loadUrl()
函数。
答案 1 :(得分:0)
方法loadUrl()
不是阻塞调用,因此您无需从后台线程调用它。如果要捕获页面加载完成的点,可以在WebviewClient
上设置WebView
对象来处理这方面的一系列回调。
此外,由于该调用未阻止,因此会立即返回,并且dismiss()
方法几乎在show()
时立即调用,这就是为什么您只是短暂显示它。
HTH