在设置的开发人员选项进度对话框中选择“不保留活动”时,不会解除。 实际上,我正在显示Progerss栏,用于初始化应用程序,当我转到活动A到活动B然后进入活动A时,进度条在初始化完成后没有解雇。 以下是我的代码,
第一项活动
package com.example.donotkeepactivitiesalive;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
public static final int MESSAGE_PROGRESS_BAR_INITIALIZING = 1;
private Dialog m_cObjDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(getClass().getName() + "before InitializeEnvironment >>>>>>");
new InitializeEnvironment().execute();
System.out.println(getClass().getName() + "after InitializeEnvironment >>>>>>");
Button lb = (Button) findViewById(R.id.button1);
lb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent lIntent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(lIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected Dialog onCreateDialog(int id) {
m_cObjDialog = new ProgressDialog(this);
m_cObjDialog.setTitle("Initializing");
((ProgressDialog) m_cObjDialog).setMessage("Initializing the Application");
m_cObjDialog.setCancelable(false);
return m_cObjDialog;
}
class InitializeEnvironment extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
System.out.println(getClass().getName() + "onPreExecute >>>>>>");
super.onPreExecute();
showDialog(MESSAGE_PROGRESS_BAR_INITIALIZING);
}
@Override
protected String doInBackground(String... aurl) {
System.out.println(getClass().getName() + " doInBackground >>>>>>");
return null;
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(MESSAGE_PROGRESS_BAR_INITIALIZING);
System.out.println(getClass().getName() + " onPostExecute >>>>>>");
}
}
}
第二项活动
package com.example.donotkeepactivitiesalive;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
是否有任何方法可以跟踪此问题,即使启用了“不保留活动”也可以正常工作
答案 0 :(得分:0)
我解决了这个问题,通过检查Bundle对象是否为null。如果Bundle对象为null则调用AsynTask类,否则不调用,
if(null == savedInstanceState) {
new InitializeEnvironment().execute();
}
但请确保您之前的活动没有覆盖onActivityResult()