2012-05-15 21 views
0

嗨每個人都在開發一個Android應用程序,其中有7個屏幕。 當我調試我的應用程序時,它工作正常,當我點擊4個屏幕上的主頁按鈕,並再次啓動我的應用程序,它從應用程序進入背景的同一屏幕/ 4屏幕開始,但是當我爲我的用戶創建app.apk文件當他們使用該應用程序並按Home鍵假設在4屏幕,並且他/她重新啓動應用程序從作爲登錄屏幕/ 1屏幕的開始屏幕開始。 任何好友都可以告訴我它有什麼問題,以及我可以如何解決這個問題。應用程序關閉所有活動,當點擊主頁鍵時?

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dblist); 
    setTitle("Databases"); 

    try { 
     jsonObj = new JSONObject(getIntent().getStringExtra("key")); 
     nameArray = jsonObj.names(); 
     valArray = jsonObj.getJSONArray("DbList"); 
    } catch (JSONException e1) { 

     e1.printStackTrace(); 
    } 

    ArrayAdapter<String> dbName = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1); 

    for (Integer i = 0; i < valArray.length(); i++) { 

     try { 
      String obj = valArray.getJSONObject(i) 
        .getString("DataBaseName").toString(); 
      dbName.add(obj); 
     } catch (JSONException e) { 

     } 

    } 
    setListAdapter(dbName); 
} 


@Override 
protected void onResume() { 

    super.onResume(); 

    // versionUpdate(); 

    Logout lo = new Logout(); 
    lo.Check(); 
    processThreadLogoutTime(); 

} 

final Handler handler = new Handler() { 

    @Override 
    public void handleMessage(Message msg) { 

     super.handleMessage(msg); 

     int compareTime = 1; 

     if (diff >= compareTime) { 

      Intent intent = new Intent(ShowDbList.this, LoginActivity.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(intent); 

     } 

    } 

}; 

protected void processThreadLogoutTime() { 

    new Thread() { 

     public void run() { 

      cw = new ConnectToWebService(); 
      getMethod gm = getMethod.GetApplicationDetails; 
      String result = cw.getUrl("", gm); 
      String urlLogoutTime = result.replaceAll(" ", "%20"); 
      cw.LogoutTime(urlLogoutTime); 
      Logout logout = new Logout(); 

      diff = logout.LogoutFun(); 

      handler.sendEmptyMessage(0); 

     } 

    }.start(); 

} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 

    String item = (String) getListAdapter().getItem(position); 
    Entities.DataBaseName = item; 
    try { 
     String webAdmin = valArray.getJSONObject(position) 
       .getString("WebAdmin").toString(); 
     Integer uId = Integer.parseInt(valArray.getJSONObject(position) 
       .getString("UserID")); 
     Entities.webAdmin = webAdmin; 
     Entities.userId = uId; 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 

    } 
    Intent intent = new Intent(v.getContext(), Menu.class); 
    startActivity(intent); 

} 
+0

您可以發佈您的錯誤日誌? – Piyush

+2

您的應用程序因用戶而死亡,所以當他們再次啓動應用程序時,它會重新啓動。但是當你在你的模擬器上測試它時,它可能不會被殺死,因此它從它離開的地方恢復。而且,實際上,你無法提供幫助,Android在有需求時會殺死應用程序(如內存)。 –

+1

當你從一個活動轉移到另一個活動或請分享活動4的代碼時,你正在使用完成()。 –

回答

0

Kazekage Gaara是對的:你的應用程序被殺死。如果你堅持從最後一個地方啓動應用程序,即使它已關閉,你也需要處理它。無論何時調用onStop,都要持久保存狀態,並確保將用戶發送到onCreate/onStart中的正確位置。

0

僅用於測試目的,我允許我的應用APK通過URL下載並安裝。一旦在手機上下載,就可以通過Android應用安裝程序啓動,該應用程序安裝程序可讓用戶選擇將其安裝到他們的設備,然後運行它。

請考慮我們是否按照上述方式下載並運行了應用程序。我的應用程序中的主/啓動器活動是一個登錄頁面(Activity Act1)。一旦用戶被認證,它們被帶到應用的主要區域,例如,活動Act2。所以現在這個任務的當前活動堆棧是Act1> Act2。

我然後在手機上按home鍵和我帶到Android主屏幕。我通過菜單中的圖標重新啓動我的應用程序,並將其帶到活動A而不是活動B.現在活動堆棧現在是Act1> Act2> Act1,或者現在有兩個單獨的任務,活動堆棧Act1> Act2和Act1。當我重新啓動應用程序時,我想要返回到活動B.在這種狀態下退後會讓我回到活動法2。

此意外的行爲只發生,如果我首先通過安裝程序打開應用程序,而不是如果我通過主屏幕/菜單中打開應用程序。

這是由於所使用的意圖來啓動應用程序是不同的。 Eclipse啓動一個應用程序,使用一個沒有操作和沒有類別的意圖。啓動器啓動一個應用程序,使用意圖與android.intent.action.MAIN行動和android.intent.category.LAUNCHER類別。安裝程序使用android.intent.action.MAIN操作和類別啓動應用程序。

只要保持逼人退,完全從Eclipse中部署它之後退出應用程序。從手機再次啓動。這解決了您的問題,但這是一個曇花一現的解決方案。

檢查FLAG_ACTIVITY_BROUGHT_TO_FRONT的偉大工程。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { 
     // Activity was brought to front and not created, 
     // Thus finishing this will get us to the last viewed activity 
     finish(); 
     return; 
    } 

    // Regular activity creation code... 
} 
相關問題