2011-01-26 2 views
2

검색 버튼을 누르면 Android Search 상자가 표시되는 MapActivity가 있습니다. SearchManager는 대화 상자를 관리하고 SQLite DB를 검색하고 사용자 정의 어댑터를 사용하여 결과를 표시하는 검색 가능한 활동에 사용자의 쿼리를 전달합니다.Android onSearchRequested() 호출 작업에 대한 콜백

이 작동합니다. 그리고 표시된 DB에서 올바른 결과를 얻고 있습니다.

그러나 사용자가 검색 결과를 클릭하면 Map의 MapActivity에 결과를 표시하려고합니다. 현재는 새로운 MapActivity를 시작하고 Bundle을 사용하여 검색 결과를 전달합니다.

나는 더 깨끗한 방법으로 검색 결과를 새로운 활동을 시작하기보다는 원래 활동으로 되돌려 보내는 것이라고 생각했습니다. 현재 활동 스택은 MapAct -> SearchManager -> Search Result -> New MapAct입니다. 즉, 새 MapAct에서 '뒤로'를 누르면 쿼리 결과로 돌아가고 원래 MapAct로 돌아갑니다.

검색 결과에서 finish()를 호출해도 호출하는 MapActivity에서 onActivityResult가 호출되지 않는 것으로 보입니다.

이 콜백을 얻고 적절한 액티비티 스택을 유지하는 방법에 대한 아이디어가 있으십니까?

답변

5

나는이 정확한 질문에 대한 답을 찾기 위해 파고 들었고 마침내 작동하는 것을 발견했다. 나는 원래 호출 활동 또한 검색 가능한 활동을했습니다, 그래서 매니페스트 내 항목은 다음과 같습니다

<activity android:name=".BaseActivity" 
      android:launchMode="singleTop"> 
    <!-- BaseActivity is also the searchable activity --> 
    <intent-filter> 
     <action android:name="android.intent.action.SEARCH" /> 
    </intent-filter> 
    <meta-data android:name="android.app.searchable" 
       android:resource="@xml/searchable"/> 
    <!-- enable the base activity to send searches to itself --> 
    <meta-data android:name="android.app.default_searchable" 
       android:value=".BaseActivity" /> 
</activity> 

그리고를 다음 대신 실제 검색 활동을 수동으로,이 활동에 startActivityForResult를 검색하는, 어떤 setResultfinish을 원래 통화 활동으로 되돌릴 수 있습니다.

나는 조금 더 자세하게 blog post here에 들어갔다.

1

마침내 singleTop과 관련없는 해결책을 발견했습니다. 그 플래그가있는 경우

@Override 
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) { 
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     int flags = intent.getFlags(); 
     // We have to clear this bit (which search automatically sets) otherwise startActivityForResult will never work 
     flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK; 
     intent.setFlags(flags); 
     // We override the requestCode (which will be -1 initially) 
     // with a constant of ours. 
     requestCode = AppConstants.ACTION_SEARCH_REQUEST_CODE; 
    } 
    super.startActivityForResult(intent, requestCode, options); 
} 

안드로이드는 항상 (어떤 이유로) 어떤 이유로 Intent.FLAG_ACTIVITY_NEW_TASK 플래그와 함께 ACTION_SEARCH 의도를 시작되지만 :

첫째, 검색을 기원 당신의 활동에,에 startActivityForResult를 오버라이드 (override) 설정하면 onActivityResult은 원래 작업에서 (올바르게) 호출되지 않습니다.

다음으로 검색 가능한 활동에서 사용자가 항목을 선택할 때 보통 setResult(Intent.RESULT_OK, resultBundle)을 호출하기 만하면됩니다.

마지막으로, 당신은 당신의 원래 활동에 onActivityResult(int requestCode, int resultCode, Intent data)을 구현하고 resultCodeIntent.RESULT_OK하고 requestCode 귀하의 요청 코드 상수 (이 경우 AppConstants.ACTION_SEARCH_REQUEST_CODE) 때 적절하게 반응한다.

관련 문제