Android : 검색을 처리하는 하나의 단일 활동으로 검색 쿼리를 전달합니다.

StackOverflow https://stackoverflow.com/questions/1906964

문제

활동 처리 검색이 있습니다 (활동 _1),이 활동 내/휴대 전화의 검색 버튼을 통해 검색을 사용할 때 완벽하게 작동합니다.

그러나 다른 활동에서 검색을 사용할 때 (Activity_2..X) 구현함으로써 onNewIntent 쿼리 문자열을 내 search_activity.class로 전달합니다.활동 _1)

@Override
protected void onNewIntent(Intent intent) {
    Log.i(TAG, "onNewIntent()");

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        Log.i(TAG, "===== Intent: ACTION_SEARCH =====");
        Intent myIntent = new Intent(getBaseContext(), Search_Activity.class);
        myIntent.setAction(Intent.ACTION_SEARCH);
        myIntent.putExtra(SearchManager.QUERY, intent.getStringExtra(SearchManager.QUERY));
        startActivity(myIntent);
    }

}

항상 일시 중지됩니다 활동 _2 먼저 oncreate ()로갑니다 활동 _2.

  • 왜 내 재현? 활동 _2 이미 거기에 있고 직접적으로 오지 않을 때?
  • 검색 쿼리를 직접 전달할 수있는 다른 방법이 있습니까? 활동 _1? 예를 들어 Manifest.xml의 설정을 통해
  • 일반적으로 모든 검색 쿼리를 자동으로 전달할 수 있습니까? 활동 _1 조차 구현하지 않고 onNewIntent 다른 모든 활동에서?

현재 나는 넣어야한다 <intent-filter> 모든 단일 활동에서 내 사용자 정의 검색을 "활성화"하고 쿼리를 전달 한 다음 검색을 처리하는 활동으로 전달합니다. onNewIntent (위와 같이).

<activity android:name=".Another_Activity"
    android:theme="@style/MyTheme">
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
        android:resource="@xml/searchable" />
</activity>
도움이 되었습니까?

해결책

설명하는 이벤트 체인을 이해하는 것은 확실하지 않지만, 여기에서 응용 프로그램을 구성 해야하는 방법은 Activity_1이 사용자가 '검색'을 누를 때 항상 다른 모든 활동에서 시작하려는 검색 활동입니다. 단추.

검색 버튼이 Activity1에서 완벽하게 작동한다고 가정하면, 아래의 매니페스트 스 니펫에 표시된 것처럼 다른 모든 활동을 검색에 사용해야한다고 말하면서 응용 프로그램에 약간의 접착제 메타 데이터를 추가하면됩니다.

<application>
  <meta-data 
     android:name="android.app.default_searchable"
     android:value=".ACTIVITY_1" />

   <!-- All your activities, service, etc. -->

</application>

이것을 사용하면 Activity_1을 제외한 모든 의도 필터를 제거 할 수 있어야하며 사용할 필요가 없습니다. onNewIntent 다른 활동의 핸들러.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top