Search button is bad
<p>I have a class that extends TabActivity and has two tabs (Contacts and another):</p>

<pre><code>    final TabHost tabHost = getTabHost();
    tabHost.addTab(tabHost.newTabSpec("contacts")
            .setIndicator("Contacts", getResources().getDrawable(R.drawable.param_icon))
            .setContent(new Intent(this, Contacts.class)));
    tabHost.addTab(tabHost.newTabSpec("other")
            .setIndicator("Other", getResources().getDrawable(R.drawable.param_icon))
            .setContent(new Intent(this, Other.class)));
</code></pre>

<p>Contacts.class extends Activity and has a tabHost:</p>

<pre><code>    setContentView(R.layout.contacts_layout);
    TabHost tabHost = (TabHost) findViewById(R.id.tabhost);   

    LocalActivityManager mlam = new LocalActivityManager(this, false);
    mlam.dispatchCreate(savedInstanceState);
    tabHost.setup(mlam);
    TabHost.TabSpec spec;

    Intent intentA = new Intent().setClass(this, A.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec = tabHost.newTabSpec("A").setIndicator("A").setContent(intentA);
    tabHost.addTab(spec);

    Intent intentB = new Intent().setClass(this, B.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec = tabHost.newTabSpec("B").setIndicator("B").setContent(intentB);
    tabHost.addTab(spec);

    Intent intentC = new Intent().setClass(this, C.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec =tabHost.newTabSpec("C").setIndicator("C").setContent(intentC);
    tabHost.addTab(spec);
</code></pre>

<p>contacts_layout (appear three tabs on the bottom):</p>

<pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
    &lt;TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"&gt;
&lt;RelativeLayout
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"&gt;
    &lt;TabWidget
        android:id="@android:id/tabs"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" /&gt;
    &lt;FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp"
        android:layout_above="@android:id/tabs"&gt;            
   &lt;/FrameLayout&gt;

&lt;/RelativeLayout&gt;
</code></pre>

<p></p>

<p>A.class extends Activity and has a listview with its ContactListAdapter. It works fine because it shows contacts' list good. </p>

<p>PROBLEM: In this class I added a Search button in menu options and it don't work. This is my code:</p>

<p>AndroidManifest:</p>

<pre><code>      &lt;activity android:name=".A"&gt;
        &lt;intent-filter&gt;         
            &lt;action android:name="android.intent.action.VIEW"/&gt;
            &lt;category android:name="android.intent.category.DEFAULT"/&gt;
        &lt;/intent-filter&gt;
        &lt;intent-filter&gt;
            &lt;action android:name="android.intent.action.SEARCH"/&gt;
        &lt;/intent-filter&gt;
          &lt;meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable"/&gt;

    &lt;/activity&gt;
</code></pre>

<p>Searchable:</p>

<pre><code>          &lt;?xml version="1.0" encoding="utf-8"?&gt;
          &lt;searchable xmlns:android="http://schemas.android.com/apk/res/android"
               android:label="@string/app_name"
               android:hint="@string/search" &gt;
          &lt;/searchable&gt;
</code></pre>

<p>Code in A.class:</p>

<pre><code>   public class A extends Activity{

    ListView list;
ContactListAdapter ad;
int textlength=0;
String[] PROJECTION = new String[] {
        Phone._ID,
        Phone.NUMBER,
        Phone.LABEL,
        Phone.TYPE,
        Phone.CONTACT_ID
    };
MatrixCursor matrix = new MatrixCursor(PROJECTION);

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);      
  setContentView(R.layout.contacts_list);

  list = (ListView)findViewById(R.id.ListContacts);
      Intent search = getIntent();
  if (Intent.ACTION_SEARCH.equals(search.getAction())) {
      String query = search.getStringExtra(SearchManager.QUERY);
      list.setAdapter(doMySearch(query));
  }
      ad=setAllContacts(); 

  list.setAdapter(ad);
    }

    public ContactListAdapter doMySearch(String query){
    Cursor contacts = ad.getCursor();
    while (contacts.moveToNext()){
        String phone = PhoneUtils.formatNumberToInternational(contacts.getString(1));
        String name = Utils.contactName(this.getContentResolver(),phone);
        if (textlength &lt;= name.length()){
            if(query.equalsIgnoreCase((String)name.subSequence(0,textlength))){
                matrix.addRow(new Object[]{contacts.getLong(0), 
                        phone,
                        contacts.getString(2),
                        contacts.getInt(3),
                        contacts.getLong(4)});
            }
        }
    }
    contacts.close();

    return new ContactListAdapter(this, matrix);
}

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater=new MenuInflater(getApplicationContext());
    inflater.inflate(R.menu.options_menu, menu);
    return true;
}

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

          case R.id.menu_search_contact:
          if (!isFinishing()){
        onSearchRequested();
          }
      break;

    }
    return true;
}

     @Override
public boolean onSearchRequested(){
     startSearch("initial query", false, null, false);
        return true;
}
</code></pre>

<p>When press search button it throws: android.view.WindowManager$BadTokenException: </p>

<blockquote>
  <p>Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@40529688 is not valid; is your activity running? </p>
</blockquote>

<p>WHY?????????????????????????????????????</p>

<p>NOTE: I know that it's better use:</p>

<pre><code>   handleIntent(getIntent());

    protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      String query = intent.getStringExtra(SearchManager.QUERY);
      list.setAdapter(doMySearch(query));
    }
}
</code></pre>

<p>But I need that A.class has FLAG_ACTIVITY_CLEAR_TOP not android:launchMode="singleTop". How do it??</p>
