Android 从家返回我的活动时,执行多个onResume()
当我从我的(主)活动中导航出来,然后单击主屏幕上的图标返回时,该活动将自动恢复->暂停->恢复。 我只希望有一个onResume()。 My activity在onResume()函数中创建了一个AsyncTask(该活动根本没有调用其他活动),目前创建了两个额外的AsyncTask,而不是一个。 我做了一些测试,并注意到当这个活动在Mainfest中声明为“SingleTask”时会发生这种情况。使用“SingleTop”时,一切正常,onResume()只调用一次 救命啊 这是我的主要活动代码:Android 从家返回我的活动时,执行多个onResume(),android,android-asynctask,android-activity,Android,Android Asynctask,Android Activity,当我从我的(主)活动中导航出来,然后单击主屏幕上的图标返回时,该活动将自动恢复->暂停->恢复。 我只希望有一个onResume()。 My activity在onResume()函数中创建了一个AsyncTask(该活动根本没有调用其他活动),目前创建了两个额外的AsyncTask,而不是一个。 我做了一些测试,并注意到当这个活动在Mainfest中声明为“SingleTask”时会发生这种情况。使用“SingleTop”时,一切正常,onResume()只调用一次 救命啊 这是我的主要活动代
public class HomeFinderActivity extends ListActivity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
private Location location;
private static final String LOG_TAG = "::HomeFinderActivity->Asynctask";
private ArrayList<Home> home_parts = new ArrayList<Home>();
private ListViewAdapter m_adapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.main);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
// instantiate ListViewAdapter class
m_adapter = new ListViewAdapter(this, R.layout.row, home_parts);
setListAdapter(m_adapter);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
}
//Asynctask to retrieve cursor from database and sort list by distance
private class SortList extends AsyncTask<Location, Void, ArrayList<Home>> {
@Override
protected ArrayList<Home> doInBackground(Location... location) {
try {
if (home_parts.isEmpty()){
home_parts=Home.getHomeParts(location[0], getApplicationContext());
}
else{
for (Home d : home_parts){
if (location != null){
d.setmDistance((int) (d.getmLatitude()), d.getmLongitude(),(double) (location[0].getLatitude())
, (double) (location[0].getLongitude()));
}
}
}
} finally {
}
Collections.sort(home_parts, new Comparator(){
public int compare(Object o1, Object o2) {
Home p1 = (Home) o1;
Home p2 = (Home) o2;
return (int) p1.getmDistance()- (int) p2.getmDistance();
}
});
return home_parts;
}
protected void onPostExecute(ArrayList<Home> address) {
m_adapter = new ListViewAdapter(HomeFinderActivity.this, R.layout.row, address);
// display the list.
setListAdapter(m_adapter);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
//starting handling location
/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
Log.e(LOG_TAG, "onResume() started");
if (location != null) {
onLocationChanged(location);
}
else
{
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
Log.e(LOG_TAG, "onPause() started");
locationManager.removeUpdates(this);
}
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
SortList showList = new SortList();
showList.execute(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
/** Called when the user clicks the Add Entry button */
public void goAddEntry(View view) {
Intent intent = new Intent(this, AddEntry.class);
startActivity(intent);
}
}
公共类HomeFinderActivity扩展ListActivity实现LocationListener{
私有文本视图latituteField;
私有文本视图纵向字段;
私人场所经理场所经理;
私有字符串提供者;
私人位置;
私有静态最终字符串LOG_TAG=“::HomeFinderActivity->Asynctask”;
private ArrayList home_parts=new ArrayList();
私有ListViewAdapter m_adapter;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent=getIntent();
setContentView(R.layout.main);
latituteField=(TextView)findViewById(R.id.TextView02);
longitudeField=(TextView)findViewById(R.id.TextView04);
//实例化ListViewAdapter类
m_adapter=新的ListViewAdapter(此,R.layout.row,主零件);
setListAdapter(m_适配器);
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
//定义如何选择位置提供程序->使用的条件
//违约
标准=新标准();
provider=locationManager.getBestProvider(条件,false);
location=locationManager.getLastKnownLocation(提供者);
}
//Asynctask从数据库检索光标并按距离对列表排序
私有类SortList扩展了AsyncTask{
@凌驾
受保护的ArrayList doInBackground(位置…位置){
试一试{
if(home\u parts.isEmpty()){
home\u parts=home.getHomeParts(位置[0],getApplicationContext());
}
否则{
用于(主d:主零件){
如果(位置!=null){
d、 setmDistance((int)(d.getmLatitude())、d.getmLongitate()、(double)(位置[0].getLatitude())
,(双精度)(位置[0].getLongitude());
}
}
}
}最后{
}
Collections.sort(home\u部件,新的Comparator(){
公共整数比较(对象o1、对象o2){
原点p1=(原点)o1;
家p2=(家)o2;
返回(int)p1.getmDistance()-(int)p2.getmDistance();
}
});
返回家中的零件;
}
受保护的void onPostExecute(ArrayList地址){
m_adapter=新的ListViewAdapter(HomeFinderActivity.this,R.layout.row,address);
//显示列表。
setListAdapter(m_适配器);
}
}
@凌驾
受保护的空onDestroy(){
//TODO自动生成的方法存根
super.ondestory();
}
//起始处理位置
/*启动时请求更新*/
@凌驾
受保护的void onResume(){
super.onResume();
e(Log_标记,“onResume()已启动”);
如果(位置!=null){
onLocationChanged(位置);
}
其他的
{
latituteField.setText(“位置不可用”);
longitudeField.setText(“位置不可用”);
}
locationManager.requestLocationUpdates(提供者,400,1,this);
}
/*活动暂停时删除locationlistener更新*/
@凌驾
受保护的void onPause(){
super.onPause();
Log.e(Log_标记,“onPause()已启动”);
locationManager.RemoveUpdate(此);
}
已更改位置上的公共无效(位置){
intlat=(int)(location.getLatitude());
int lng=(int)(location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(字符串值(lng));
SortList showList=新SortList();
showList.execute(位置);
}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
//TODO自动生成的方法存根
}
公共无效onProviderEnabled(字符串提供程序){
}
公共无效onProviderDisabled(字符串提供程序){
}
/**当用户单击“添加条目”按钮时调用*/
公共无效目标入口(视图){
意向意向=新意向(此,AddEntry.class);
星触觉(意向);
}
}