How to Start an Intent by Passing Some Parameters to It

How to start an Intent by passing some parameters to it?

In order to pass the parameters you create new intent and put a parameter map:

Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("firstKeyName","FirstKeyValue");
myIntent.putExtra("secondKeyName","SecondKeyValue");
startActivity(myIntent);

In order to get the parameters values inside the started activity, you must call the get[type]Extra() on the same intent:

// getIntent() is a method from the started activity
Intent myIntent = getIntent(); // gets the previously created intent
String firstKeyName = myIntent.getStringExtra("firstKeyName"); // will return "FirstKeyValue"
String secondKeyName= myIntent.getStringExtra("secondKeyName"); // will return "SecondKeyValue"

If your parameters are ints you would use getIntExtra() instead etc.
Now you can use your parameters like you normally would.

How do I pass data between Activities in Android application?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);

Access that intent on the next activity:

String sessionId = getIntent().getStringExtra("EXTRA_SESSION_ID");

The docs for Intents has more information (look at the section titled "Extras").

Start an Activity with a parameter

Put an int which is your id into the new Intent.

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("key", 1); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
finish();

Then grab the id in your new Activity:

Bundle b = getIntent().getExtras();
int value = -1; // or other values
if(b != null)
value = b.getInt("key");

Opening/starting a specific Android activity passing an intent parameter from the browser

You have 4 options to achieve what you want:

  1. Deep Links
  2. Android App Links
  3. Firebase Dynamic Links
  4. Firebase App Indexing

Refer to this post to see more descriptions about them.

In simplest approach (Deep Links), you can introduce your Activity as a handler of specific pattern URLs and pass the desired parameters as URL query params.

AndroidManifest.xml

<activity android:name=".SphereViewerActivity">

<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<!-- Accepts URIs that begin with "myapp://zxing" -->
<data android:host="zxing" />
<data android:scheme="myapp" />
</intent-filter>

</activity>

SphereViewerActivity.kt

class SphereViewerActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sphere_viewer)

if (intent != null && intent.action == Intent.ACTION_VIEW) {
intent.data?.apply{
if (getQueryParameter("image_url") != null && getQueryParameter("image_url").isNotEmpty()) {
val imageUrl = data.getQueryParameter ("image_url") as String
// do what you want to do with imageUrl
}
}
}
}
}

Your html snippet:

<a href="myapp://zxing?image_url=some_image_url"> Take a QR code </a>

How can I pass parameters between two Activity but one must only support the first one

From your MainActivity call the TargetActivity using startActivityForResult()-

For example:

Intent intent = new Intent(this, TargetActivity.class);
intent.putExtra(); // sent your putExtra data here to pass through intent
startActivityForResult(intent, 1000);

In your intent set the data which you want to return back to MainActivity. If you don't want to return back any data then you don't need to set any data.

For example:

In TargetActivity if you want to send back data:

Intent returnIntent = new Intent();
returnIntent.putExtra("result", result);
setResult(Activity.RESULT_OK, returnIntent);
finish();

If you don't want to return data:

Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();

Now in your MainActivity class write following code for the onActivityResult() method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1000) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == Activity.RESULT_CANCELED) {
// Do your task here.
}
}
}

How can i pass arguments from my native code to flutter via intent?

Well.. you don't (need to). Simply encode everything into the root path.. like /specific_route_name?foo=bar and then use something like:

    final uri = Uri.parse(settings.name);
final route = uri.path;
final param = uri.queryParameters['foo'];

this way you will have your route name in route and can read out your parameters through uri.queryParameters.

Android: start service with parameter

Step #1: Delete your BroadcastReceiver implementation.

Step #2: Examine the Intent your service gets in onStartCommand() and look at the action via getAction().

Android: Intent with parameters

Look at the API entry for Intent. You've got a load of possible data types you can enter, not the least of which is Parcebles, Bundles, and Serializable. If you really want simple object marshalling I would convert your beans to JSON and put it as a String, then convert it back to a POJO on the receiving end.



Related Topics



Leave a reply



Submit