为什么OnCreate()调用两次但savedInstance仍为null

时间:2014-04-06 00:11:17

标签: android oncreate savestate

我有ListActivty onCreate(),其中设置了一个列表适配器,其中一个游标对象包含数据库中的所有数据。

我在这个ListActivity中有一个搜索方法,它调用另一个数据库活动中的方法从搜索中检索Cursor,并将光标返回给我的ListActivity。

然而,在调用此方法后,再次调用ListActivty onCreate(),再次调用包含所有数据的原始列表适配器,因此当我尝试使用搜索光标设置适配器时,我永远看不到搜索结果

我尝试在savedInstance!= null时设置一个布尔标志来设置正确的搜索适配器,但savedInstance总是保持为空......

所以我的问题是,当onCreate()被调用两次时,活动实际发生了什么?

编辑:添加了代码 onCreate()我设置了列表apdter

public class ViewListOfTests  extends ListActivity implements SearchView.OnQueryTextListener, SearchView.OnCloseListener{.....

    this.setContentView(R.layout.list_activity);

                     // Associate searchable configuration with the SearchView
                     searchView = (SearchView) findViewById(R.id.search);
                        searchView.setIconifiedByDefault(false);
                        searchView.setOnQueryTextListener(this);
                        searchView.setOnCloseListener( this);

                    SearchManager searchManager =
                           (SearchManager) getSystemService(Context.SEARCH_SERVICE);
                    //SearchView searchView =(SearchView) menu.findItem(R.id.search).getActionView();
                    searchView.setSearchableInfo(
                            searchManager.getSearchableInfo(getComponentName()));

    //(SearchView) this.findViewById(id) menu.findItem(R.id.search).getActionView();


            /*call the aysnch inner class to load cursor form Db and set teh customer adter to this list view
            *off the main UI thread, cast to type (getCursor)
            *Asynch class does not have a constructor in this case
            **/

                    if(savedInstanceState!=null){
                        //getCursorAysnch = (getCursor) new getCursor().execute();
                        searchPerformed=true;
                    }
            //getCursorAysnch = (getCursor) new getCursor().execute();
                    try{
                    noOftimesOnCreateCalled = savedInstanceState.getInt("MyInt");
                    searchPerformed = savedInstanceState.getBoolean("MyBoolean");
                    }catch(Exception e){
                        e.printStackTrace();
                        Log.d("VIEWLISTTESTS", "noOFTOMESONCREATECALLED NOT CALLED");
                    }



                    if (noOftimesOnCreateCalled>0){
                        this.setListAdapter(searchAdapter);
                        Log.d("VIEWLISTOFDIVES", "IN ONCTREATE SERACHPERFOMED + TRUE");
                    }else if(noOftimesOnCreateCalled==0){
                        getCursorAysnch = (getCursor) new getCursor().execute();
                        Log.d("VIEWLISTOFDIVES", "IN ONCTREATE SERACHPERFOMED + FALSE");
                        noOftimesOnCreateCalled++;
                    }

我得到搜索查询fem一个searchView小部件并使用查询调用以下方法,在此方法之后再次调用oCreate()...

@Override

public boolean onQueryTextChange(String newText) {
    // TODO Auto-generated method stub
    //handleIntent(intent);
    Log.d("LIST ACTIVITY SEARCH", "onQueryTextCahanged called: "+ newText);
    data = new database(this);
    Cursor c = data.getWordMatches(newText, null);

    Log.d("LISTACTIVITY", "CURSOR QUERY ROWS/COLS = "+ c.getCount()+" "+c.getColumnCount());

    //now set bind cursor data to list view using custim ItemAdpter class
    //ItemAdapter newAdapter = new ItemAdapter(ViewListOfTests.this, c);
    //newAdapter.notifyDataSetChanged();
    //ViewListOfTests.this.setListAdapter(newAdapter);
    searchAdapter= new ItemAdapter(this, c);
    //this.setListAdapter(searchAdapter);
    searchPerformed=true;
    return false;
}

尽管savediNstanceState始终保持无效,但我尝试在此处保存变量:

@Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);
      // Save UI state changes to the savedInstanceState.
      // This bundle will be passed to onCreate if the process is
      // killed and restarted ie when we go ot ItemAdter to craete a new adpter with serach 

      //savedInstanceState.putBoolean(key, false)
      savedInstanceState.putBoolean("MyBoolean", true);
      savedInstanceState.putDouble("myDouble", 1.9);
      savedInstanceState.putInt("MyInt", 1);
      savedInstanceState.putString("MyString", "Welcome back to Android");
      // etc.
    }


    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      // Restore UI state from the savedInstanceState.
      // This bundle has also been passed to onCreate.
      searchPerformed = savedInstanceState.getBoolean("MyBoolean");
      double myDouble = savedInstanceState.getDouble("myDouble");
       noOftimesOnCreateCalled = savedInstanceState.getInt("MyInt");
      String myString = savedInstanceState.getString("MyString");
    }

2 个答案:

答案 0 :(得分:1)

为了使onCreate() savedInstanceState非空,您必须在Android强制退出活动时为Android提供已保存的状态。

为此,您必须覆盖void onSaveInstanceState(Bundle savedInstanceState)方法并使用您想要的任何状态填写传入的savedInstanceState Bundle。

答案 1 :(得分:0)

问题是我有一些代码用于在用户提交查询时使用ACTION_SERACH意图配置可搜索的窗口小部件:

@Override
    protected void onNewIntent(Intent intent) {
        // call handleIntent from here to handle the query from serah widget

        super.onNewIntent(intent);

        handleIntent(intent);

    }//end onNewIntent


// Associate searchable configuration with the SearchView
    SearchManager searchManager =
           (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

我忘记删除此代码,因为它的目的不是使用ACTION_SEARCH意图配置搜索视图,但我相信这个意图已被调用,然后再次调用onCreate()。