活动和垃圾收集器中的android SaveInstance

时间:2018-12-25 21:19:56

标签: java android android-activity garbage-collection

目前,我正在开发第二版词汇应用程序。第一个版本没有提供足够的文档来扩展应用程序。所以我决定从头开始。

我有一个活动,每次我将其发送到后台,然后再将其返回到前台时,一些对象将变为空,活动将重新启动。在onResume中,我记录了null对象的值。

旧活动(版本1)没有这个问题。

在将活动状态和对象保存到SaveInstanceState并还原它们的同时,我尝试避免了该空问题。但从来没有调用RestoreInstance。

这是我的代码

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_query);


    Log.e(TAG, "onCreate");

    /* Initialize DATA */
    data = new DataHandler(this);


    /* Initialize VIEWS */
    btnPhase  = findViewById(R.id.query_voc_phase);
    cardPath  = findViewById(R.id.query_voc_path);
    queryView = findViewById(R.id.query_view);
    btnShow   = findViewById(R.id.query_btn_show_answer);
    btnRight  = findViewById(R.id.query_btn_mark_right);
    btnWrong  = findViewById(R.id.query_btn_mark_wrong);
    btnNext   = findViewById(R.id.query_btn_show_next);
}

@Override
protected void onStart() {
    super.onStart();

    Log.e(TAG, "onStart");
    /* set OnClickListener */
    btnShow.setOnClickListener(this);
    btnRight.setOnClickListener(this);
    btnWrong.setOnClickListener(this);
    btnNext.setOnClickListener(this);


    //init Variables
    queryList       = new ArrayList<>();
    idList           = new ArrayList<>();
    listGroupings   = new ArrayList<>();
    answeredList    = new ArrayList<>();


    //init Preferences
    preferences = PreferenceManager.getDefaultSharedPreferences(this);

    //set Visibility of view items based on Preferences
    if (!preferences.getBoolean(Preferences.SHOW_CARD_PATH_ENABLED, true))
        cardPath.setVisibility(View.GONE);

    //load preferences
    userID = preferences.getInt(Preferences.CURRENT_USER, -1);

    //check if flashing card is enabled and get time
    flash_time = preferences.getBoolean(Preferences.FLASH_CARD_ENABLED, false) ? preferences.getInt(Preferences.FLASH_TIME, flash_time) : -1;


    //get data from intent
    Intent intent = getIntent();
    if (intent.hasExtra(QUERY_CONTAINER_LIST)) {
        //get data into the list
        HashMap<String, ArrayList<Integer>> queryData = data.getCardsToQuery(intent.getStringArrayListExtra(QUERY_CONTAINER_LIST));

        idList = queryData.get("iList");
        listGroupings = queryData.get("listGroupings");
    }


    // change active Fragment to initial fragment
    changeFragment(QueryViews.Simple);
}

@Override
protected void onResume() {
    super.onResume();

    Log.e(TAG, "onResume");
    Log.e(TAG, "current card is null " + (currentCard == null));

    //loads a new card if there is none
    if(currentCard == null) loadNext();
}

/*
    InstanceState
 */

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    Log.e(TAG, "RESTORE");

    queryList = savedInstanceState.getParcelableArrayList("queryList");
    idList = savedInstanceState.getIntegerArrayList("iList");
    listGroupings = savedInstanceState.getIntegerArrayList("listGroupings");
    answeredList = savedInstanceState.getParcelableArrayList("answeredList");

    currentCard = (VocabularyCard)savedInstanceState.getSerializable("currentCard");
    lastCard =  (VocabularyCard)savedInstanceState.getSerializable("lastCard");
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    Log.e(TAG, "SAVE");

    outState.putParcelableArrayList("queryList", queryList);
    outState.putIntegerArrayList("idList", idList);
    outState.putIntegerArrayList("listGroupings", listGroupings);
    outState.putParcelableArrayList("answeredList", answeredList);

    outState.putParcelable("currentCard", currentCard);
    outState.putParcelable("lastCard", lastCard);

    super.onSaveInstanceState(outState);
}

2 个答案:

答案 0 :(得分:0)

发现以下错误,可能是拼写错误,但可能是空对象问题的元凶。您在file_get_contents内的iList

onRestoreInstanceState

但是在@Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); Log.e(TAG, "RESTORE"); idList = savedInstanceState.getIntegerArrayList("iList"); } 中,您放了onSaveInstanceState

idList

答案 1 :(得分:0)

我找到了解决方案。我发现如果设备旋转,将会调用onRestoreInstanceState。首先,如果某些内容变为空,则应首先检查是否为对象分配了值。在这种情况下,我忽略了该对象从未分配值。