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);
}
}