1

當在設置的開發人員選項進度對話框上選擇「不保持活動」時不會退出。 實際上,我正在顯示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

你得到任何錯誤... ??? – Developer 2013-03-21 12:46:49

+0

Log Cat沒有錯誤 – 2013-03-21 12:54:36

+0

什麼是InitializeData();方法...??? – Developer 2013-03-21 12:56:32

回答

0

我解決了這個問題,通過檢查Bundle對象是否爲null。如果Bundle對象爲null,則調用AsynTask類,否則不要調用,

if(null == savedInstanceState) { 
     new InitializeEnvironment().execute(); 
    } 

但要確保你以前的活動沒有覆蓋onActivityResult()