开发者

How can I finish the current activity when performing a search in Android?

开发者 https://www.devze.com 2023-01-29 22:23 出处:网络
I have search functionality in my app. When the search bar is invoked from a \"show activity\" and the user performs a search, I want to finish the开发者_如何学Python show activity and take the user t

I have search functionality in my app. When the search bar is invoked from a "show activity" and the user performs a search, I want to finish the开发者_如何学Python show activity and take the user to the search activity. Is there a way to know if SearchManager.onDismissListener is invoked because of a search being performed?

What I want to avoid is to have several screens of search results > show > search results > show etc, and rather "go back" to search results whenever a search is performed, and thus only have one search activity and one show activity in the stack at any one time.


If I understand your question correctly, you want to prevent the search activity from being created multiple times.

The impact of the search dialog on your activity lifecycle in the Android docs has the explanation for what you want.

Essentially, you want to set android:launchMode to "singleTop" when defining your searchable activity in the manifest. From the example at the link, you would then have something like this:

    <activity android:name=".SearchableActivity"
              android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable"/>
    </activity>

But be careful to follow their advice for handling intents with onCreate and onNewIntent, which rather than duplicate here I'll just refer you to that link.

You can do the same for what your show activity as well with the android:launchMode="singleTop"

0

精彩评论

暂无评论...
验证码 换一张
取 消