0

Question

I'm creating an app with a session activity and a searchable activity. I'm using the search dialog by calling onSearchRequested which then launches the SearchActivity class specified in my Android.xml file.

When the user selects an item from the seach activity, I want to pass the ID of that result back to the session activity that launched it. I know I can return results if I start the activity by calling startActivityForResult and adding an onActivityResult listener to capture it. However, since I'm not directly starting the activity myself, instead using the search dialog, I'm not sure how I can do this. I considered capturing the event when the search dialog is dismissed and launching the search activity manually but I'm not sure how to prevent the default launch.

I've included the relevant parts of the code below for context.

AndroidManifest.xml

<application>
    <activity
        android:name=".SessionActivity"
        android:label="@string/title_activity_session"
        android:theme="@style/AppTheme.NoActionBar">

        <meta-data android:name="android.app.default_searchable"
            android:value=".SearchActivity" />
    </activity>
    <activity android:name=".SearchActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
    </activity>
</application>

SearchActivity.java

public class SearchActivity extends AppCompatActivity
        implements TrackFragment.OnFragmentInteractionListener {

    private final static String TAG = "SearchActivity";
    private JuukeApi juuke;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // Get the intent, verify the action and get the query
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            juuke.search(query);
            setTitle("\"" + query + "\"");
        }
    }

    public boolean onOptionsItemSelected(MenuItem item){
        setResult(RESULT_CANCELED);
        finish();
        return true;
    }

    public void onButtonPressed(View view) {
        Log.d(TAG, "Frag button pressed! " + view.getTag());
        Intent data = new Intent();
        data.putExtra("request", view.getTag().toString());
        setResult(RESULT_OK);
        finish();
    }

    public void onFragmentInteraction(String text) {
        Log.d(TAG, "Fragment interaction");
    }
}

SessionActivity.java

public class SessionActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "Activity result");
        if (resultCode == RESULT_OK) {
            Log.d(TAG, "Request received: " + data.getStringExtra("request"));
        }
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_search) {
            onSearchRequested();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
Community
  • 1
  • 1
cainy393
  • 422
  • 1
  • 4
  • 16
  • https://stackoverflow.com/questions/4807879/android-onsearchrequested-callback-to-the-calling-activity – ADM Mar 29 '20 at 15:17
  • @ADM I did consider something like that but since my session activity is already very complex I was hoping to avoid creating a new instance of it. Looks like that'll be my best option though. – cainy393 Mar 29 '20 at 17:14

1 Answers1

0

One of the answers to the question linked by @ADM turned out to be my solution. I didn't want to make the current activity into my searchable activity so instead I overrode the startActivityForResult method and it worked exactly as I wanted!

    @SuppressLint("RestrictedApi")
    @Override
    public void startActivityForResult(@RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            int flags = intent.getFlags();
            flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
            intent.setFlags(flags);
            requestCode = SEARCH_REQUEST_CODE;
        }
        super.startActivityForResult(intent, requestCode, options);
    }
cainy393
  • 422
  • 1
  • 4
  • 16