2016-06-17 79 views
0

我正在一個項目中進行兩項活動,一項顯示數據庫中的行的列表,當您單擊一個項時,它將打開一個包含可編輯元素的窗體的其他活動;如何確保finish()銷燬活動並清除所有變量?

問題是,當我關閉第二個活動並重新打開它(即使我打電話給finish()),某些變量保留了上次「會話」的數據。

所以可能的問題是:如何清除所有變量/銷燬活動?

編輯

正如我一直在讀書,並努力我仍然無法計算出真正的問題,所以有一些額外的信息:

- 自定義控制器我使用

public class CustomEditText extends EditText { 

    private boolean edited=false; 
    private boolean init; 
    private boolean justLoad; 
    private String oldText; 
    public boolean required; 

    public boolean isEdited() { 
     return edited; 
    } 

    public String getOldText() { 
     return oldText; 
    } 

    public CustomEditText(Context context) { 
     super(context); 
    } 

    public CustomEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 

    } 

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 


    @Override 
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { 
     super.onTextChanged(text, start, lengthBefore, lengthAfter); 
     if (justLoad) { 
      edited=false; 
      justLoad=false; 
     } else { 
      String currtext = this.getText().toString(); 
      if (!init) { 
       oldText = currtext; 
       init = true; 
      } 
      if (!currtext.equals(oldText)) { 
       edited = true; 
      } else { 
       edited = false; 
      } 
     } 
     Log.d("cet",this.getText().toString()+" != "+oldText+" "+edited); 
    } 

    public void load(String text){ 
     init=true; 
     oldText=text; 
     edited=false; 
     justLoad=true; 
     this.setText(text); 
    } 
} 

-Utility類(也試過非靜態的

public class CustomViewUtilities { 

    private static boolean edited=false; 
    private static ArrayList<EditText> list = new ArrayList<>(); 

    public static boolean isAnyEdited(ViewGroup view){ 
     CustomViewUtilities c = new CustomViewUtilities(); 
     c.traverseEditTexts(view); 
     return edited; 
    } 

    public static ArrayList<EditText> getList(ViewGroup view) { 
     list.clear(); 
     CustomViewUtilities c = new CustomViewUtilities(); 
     c.traverseEditTexts(view); 
     return list; 
    } 

    private EditText traverseEditTexts(ViewGroup v) 
    { 
     EditText invalid = null; 
     for (int i = 0; i < v.getChildCount(); i++) 
     { 
      Object child = v.getChildAt(i); 
      if (child instanceof EditText) 
      { 
       EditText e = (EditText)child; 
       if (e instanceof CustomAutoCompleteTextView){ 
        list.add(e); 

        if (((CustomAutoCompleteTextView) e).isEdited()) { 
         edited = true;} 
       } else if (e instanceof CustomEditText) { 
        if (((CustomEditText) e).isEdited()) { 
         edited = true;} 
        list.add(e); 
       } 
      } 
      else if(child instanceof ViewGroup) 
      { 
       invalid = traverseEditTexts((ViewGroup)child); // Recursive call. 
       if(invalid != null) 
       { 
        break; 
       } 
      } 
     } 
     return invalid; 
    } 
} 

我也試圖改變android:launchMode沒有結果。 問題是,如果CustomViewUtilities.isAnyEdited(view);方法在下一次該活動啓動時任何時候返回true,則我第二次啓動活動。

任何幫助表示讚賞。

+0

你不需要完成你的活動..使用setNotifyDataChange對知識 –

+0

閱讀關於'' – xAqweRx

+0

set launchMode to standard may help – jfxu

回答

0

到底問題是無關的android,實際問題是CustomViewUtilities.edited;巫婆原本被實例化,每次private boolean edited=false;被轉爲靜態private static boolean edited=false;,所以它永遠不會回到錯誤,永遠不會被破壞。

0

如果您將所有數據都保存在您的活動中,則不會發生這種情況。如果您從活動(應用程序類,直接從數據庫或SharedPrefs也許)讀取數據,它將保持原樣。

如果不是我的建議是這樣創造的東西的情況:

private void invalidateData(){ 
// invalidate all relevant fields (set as null or 0) 
} 

電話上的活動這種方法的onDestroy方法

0

考慮文檔調用完成方法不garantee立即銷燬。 但是對於你的情況你可以重寫onPause方法。它在活動隱藏時自動調用。如果你使用這個,你必須在onResume方法中初始化數據。 https://developer.android.com/training/basics/activity-lifecycle/pausing.html

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ... 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    // init data here 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    // destroy data here 
} 
0
yourExistingActivity.finish(); // This **requests** for freeing the memory 

注意的是,活動你打電話從被破壞的結束()方法和它所有的資源都排隊垃圾收集,以及過去曾通過本次活動的所有記憶會在下一個GC循環期間被釋放。

如果你真的想盡快撤銷內存,重寫你的活動的onDestroy方法:

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Runtime.getRuntime().gc();  //This is the key 
} 
0

重寫onDestroy()方法,在這種方法就叫finish()Runtime.getRuntime().gc()方法...

所有變量都必須分配NULL ...

我的代碼:

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    try{ 
     Runtime.getRuntime().gc(); 
     finish(); 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 
}