سؤال

I have ListActivty onCreate() where a list adapter is set with a cursor object holding all data within a database.

I have a search method in this ListActivity that calls a method in another database activity to retrieve a Cursor from a search, and returns the cursor to my ListActivity.

However after this method is called the ListActivty onCreate() is called again, where the the original list adapter with all data is called again, therefore I can never see the search results when i try to set the adapter with search cursor.

I have tried setting a boolean flag fro when savedInstance != null to set the correct search adapter but the savedInstance ALWAYS remains null......

So my question what is actually happening to the activity when the onCreate() is called twice?

EDIT: Added code onCreate() I set the list 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++;
                    }

I get the search query fem a searchView widget and call the following methow with the query, after this method the oCreate() is be called again...

@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;
}

Although savediNstanceState always remains nul I try to saved variables here:

@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

Issue was I had some code to configure the searchable widget with a ACTION_SERACH intent when user submitted query :

@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()));

I had forgotten to delete this code as wasn't aiming to configure search view with a ACTION_SEARCH intent, but I believe this intent was called and then calling the onCreate() again.

نصائح أخرى

In order for onCreate()'s savedInstanceState to be non-null, you have to provide Android with the saved state when Android forces the Activity to exit.

To do this, you must override the void onSaveInstanceState(Bundle savedInstanceState) method and fill in the passed-in savedInstanceState Bundle with whatever state you want.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top