Android的 - 从不同的活动,目的相同 [英] Android - Get same intent from different activities

查看:129
本文介绍了Android的 - 从不同的活动,目的相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正与三项活动:mainActivity,readActivity和searchActivity

i'm currently working with three activities: mainActivity, readActivity, and searchActivity.

我想readActivity得到任何其他两个activities.But的意图(带有SQLite的查询字符串)到现在为止,我已经与活动这样的工作:

I'd like readActivity to get an Intent (a String with a SQLite query) from any of the two other activities.But until now, I've worked with activities this way:

在当前的活动:

Intent intent = new Intent(this,ReadActivity.class);
String query = "SQLITE QUERY";
Intent.putExtra(intent_extra, query);
startActivity(intent);

而在接下来的活动:

And in the next activity:

Intent intent = getIntent();
String query = intent.getStringExtra(MainActivity.intent_extra);

有没有办法读一个默认的意图不点名从它来自哪里活动?一个快速的方法来检查上次活动也行!

Is there a way to read a default intent without naming the activity from where it comes from? A quick way to check the last activity would work as well!

感谢你在前进!

推荐答案

没关系所在的字符串,被用作键访问定义多余的。

It doesn't matter where the String, that is used as the key for accessing the EXTRA is defined.

您可以在资源文件中指定的EXTRA-键作为字符串,并通过接收活动访问它们:

You can specify the EXTRA-keys as Strings in a resource file and access them via the receiving activity:

资源文件的strings.xml

Resource file strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="extra_sql_query">extra.SqlQuery</string>
</resources>

接收

getIntent();
String query = intent.getStringExtra(getString(R.string.extra_sql_query);

来电

Intent intent = new Intent(this,ReadActivity.class);
String query = "SQLITE QUERY";
intent.putExtra(getString(R.string.extra_sql_query, query);
startActivity(intent);


但是,如果只有一个活动......

处理那些意图倒不如保存EXTRA-键在接收活动。

handling those intents it would be better to save the EXTRA-keys in the receiving Activity.

接收

public class ReadActivity extends Activity {
     public final static String EXTRA_SQL_QUERY = "sql.query";

getIntent();
String query = intent.getStringExtra(EXTRA_SQL_QUERY);

来电

Intent intent = new Intent(this,ReadActivity.class);
String query = "SQLITE QUERY";
intent.putExtra(ReadActivity.EXTRA_SQL_QUERY, query);
startActivity(intent);

这篇关于Android的 - 从不同的活动,目的相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆