Skip to content Skip to sidebar Skip to footer

Android - Get Same Intent From Different Activities

i'm currently working with three activities: mainActivity, readActivity, and searchActivity. I'd like readActivity to get an Intent (a String with a SQLite query) from any of the t

Solution 1:

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

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

Resource file strings.xml

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

Receiver

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

Caller

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

However if there is only one activity...

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

Receiver

publicclassReadActivityextendsActivity {
     publicfinalstaticStringEXTRA_SQL_QUERY="sql.query";

and

getIntent();
Stringquery= intent.getStringExtra(EXTRA_SQL_QUERY);

Caller

Intentintent=newIntent(this,ReadActivity.class);
Stringquery="SQLITE QUERY";
intent.putExtra(ReadActivity.EXTRA_SQL_QUERY, query);
startActivity(intent);

Solution 2:

You can definitely do this, although 1 problem is

    Intent.putExtra(intent_extra, query);

Should be

    intent.putExtra(MainActivity.intent_extra, query);

because you have to add it to your variable for the intent, not the android class, wherever you want your query string passed to an activity. Then to retrieve it, call it by

intent.getStringExtra(MainActivity.intent_extra);

as you already are.

Solution 3:

yes you can.

first, change

Intent.putExtra(intent_extra, query);

intent_extra to any value. ex.

Intent.putExtra("the_query", query);

then recieve it by its name.

Intentintent= getIntent();
Stringquery= intent.getStringExtra("the_query");

Solution 4:

Intentintent=newIntent(this,ReadActivity.class);
Stringquery="SQLITE QUERY";
Intent.putExtra(MainActivity.intent_extra, query);
startActivity(intent);

It's exactly the same almost from any other activity. See above grab the constant intent_extra ( which by the way should be capitalized) from the MainActivity

Solution 5:

you can use bundles, so in the receiving activity you invoke getIntent().getBundle() and you don't care where it came from

Post a Comment for "Android - Get Same Intent From Different Activities"