简体   繁体   English

将数据从可搜索活动返回到之前的活动

[英]Return data from searchable activity to previous activity

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.我通过调用onSearchRequested来使用搜索对话框,然后它会启动在我的 Android.xml 文件中指定的SearchActivity类。

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.当用户从搜索活动中选择一个项目时,我想将该结果的 ID 传递回启动它的会话活动。 I know I can return results if I start the activity by calling startActivityForResult and adding an onActivityResult listener to capture it.我知道如果我通过调用startActivityForResult并添加一个onActivityResult侦听器来捕获它来启动活动,我可以返回结果。 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 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搜索活动.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会话活动.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);
    }
}

One of the answers to the question linked by @ADM turned out to be my solution. @ADM 链接的问题的答案之一原来是我的解决方案。 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!我不想将当前活动变成我的可搜索活动,所以我覆盖了startActivityForResult方法,它完全按照我的startActivityForResult工作!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM