我有一个自定义的DialogFragment。我希望在切换活动时显示此对话框。
场景: - 1)在actvity1中,我启动对话框。 2)启动asynctask类。 3)在doInBackground()中进行一些身份验证。 4)从onPostExecute()开始新的活动。
问题: -
对话框在asynctask之前停止。
这是我的代码: -
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public void onPageStarted(WebView view, String url,
Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
redirected =view.getUrl();
redirected= Uri.decode(redirected);
if(redirected!=null && redirected.contains(string1))
{
myprogressDialog.show(getFragmentManager(), "Wait");
}
}
@Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
if(redirected!=null && redirected.contains(mystring) )
{
myWebView.stopLoading();
String authorizationContentString = myurl
new Authentication(Activity2.this, myprogressDialog, url).execute(authorizationString);
}
private static class ProgressDialogFragment extends DialogFragment
{
public static ProgressDialogFragment newInstance()
{
return new ProgressDialogFragment();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Translucent);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_dialog_progress, container, false);
}
}
Authentication.java
@Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
super.onPreExecute();
/* progDia.show(man, "wait");
progDia= new Utility.ProgressDialogFragment().newInstance();*/
}
public AuthenticateDevice(Details1 myContext, ProgressDialogFragment myprogressDialog, String Url)
{
// TODO Auto-generated constructor stub
mContext = myContext;
this.progDia = myprogressDialog;
this.Url = Url;
this.mActivity = myContext;
}
@Override
protected Void doInBackground(String... params)
{
// TODO Auto-generated method stub
//Some authentication
return null;
}
@Override
protected void onPostExecute(Void result)
{
// TODO Auto-generated method stub
super.onPostExecute(result);
if(progDia!=null&&progDia.isVisible())
{
progDia.dismiss();
}
Intent myIntent = new Intent(mContext, Activity2.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(myIntent);
mActivity.finish();
}
答案 0 :(得分:0)
尝试使用以下代码在异步
中显示进度对话框ProgressDialog pd;
class MYAsync extends AsyncTask<Void , Void, String>{
@Override
protected void onPreExecute() {
// pd.setTitle("Please Wait");
pd.setMessage("Working...");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(String sResponse) {
pd.dismiss();
pd.cancel();
}
}
}
答案 1 :(得分:0)
我试图重新创建它,似乎工作正常。以下方法有解释您的行为的评论。
private void myWOrk() {
try {
final ProgressDialog Dialog = new ProgressDialog(this);
Dialog.setMessage("Loading route...");
Dialog.show();
Thread.sleep(2000);// load url and redirect
//Toast.makeText(LoginActivity.this, "redirect caught", Toast.LENGTH_SHORT).show();
new AsyncTask<Object, Object, Object>() {
protected String doInBackground(Object... params) {
try {
Thread.sleep(2000);// authenticate
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Object result) {
Toast.makeText(LoginActivity.this, "onPostExecute", Toast.LENGTH_SHORT).show();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Dialog.dismiss();// start your new activity here
};
}.execute(null, null, null);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}`