开发者

How to end old Activity when Search starts new Activity

开发者 https://www.devze.com 2023-03-25 16:05 出处:网络
I have an application that presents a sort of catalog of items in a ListView.The list is rather long, so I\'ve implemented the Search capability like this:

I have an application that presents a sort of catalog of items in a ListView. The list is rather long, so I've implemented the Search capability like this:

    <activity android:name=".ItemsOverview"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <!-- enable the search dialog to send searches to ItemsSearch -->
        <meta-data android:name="android.app.default_searchable"
                   android:value=".ItemsSearch" />
    </activity>
...
...
    <activity android:name=".ItemsSearch">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable" />
    </activity>

ItemsSearch then presents the same ListView, but containing only items that match the Search criteria.

There are two fundamental problems with this:

  1. ItemsSearch is an almost duplicate of ItemsOverview (but with some enhancements for the search capability);
  2. ItemsSearch overlays ItemsOverview such that if three or four searches are done, it takes four or five presses of the Back button to get out. Not quite the desired effect. :)

I would like t开发者_C百科o, in some fashion, end the original Activity when the second Activity starts and, ideally, combine the two Classes (Overview and Search) into one. Is there a way for my Activity to detect that it's already running in another process, and to kill that other process at inception?

Once I understand this, I can probably figure out how to combine the two. What do others do when they need to utilize a filtered list?


If I understand, you would you like to clear the Activity Back Stack. Use the activity flags when you call your search Activity.

Intent intent = new Intent(this, Search.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

This is the developer page about the activity back stack.


To end old Activity when Search starts new Activity give finish() in odler activity and call new activity. Intent intent = new (Intent(MainActivity.this,GPSActivity.class); startActivityForResult(intent, ACTIVITY_GPS);
finish();

In this we can start new Activity GPSActivity and finishes MainActivity.

0

精彩评论

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

关注公众号