Start an Activity with a Parameter

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

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 next activity:

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

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

How to pass an Activity as parameter in function

Maybe you can try this:

private void sendUserToActivity(Class<? extends Activity> activityClass) {
Intent intent = new Intent(RegisterActivity.this, activityClass);
startActivity(intent);
}

This way you're using generics and everyone that extends activity will be accepted, so your code is adaptable to every activity that you want.

How to send an Activity as parameter to a method?

You should use activity.getClass() instead of activity.class.

Start an Activity based on parameter passed from JavaScript interface

Android JavaScript interface supports parameter passing.

You can create a generic method which accepts String, get the respective Class and use it on startActivity(). Note that you have to use fully-qualified name of the Activity, which means including the package name (e.g. com.example.About).

Inside WebAppInterface,

@JavascriptInterface
public void openActivity(String activityName) {
String packageName = "com.example";
try {
Class activityClass = Class.forName(packageName + "." + activityName);
mContext.startActivity(new Intent(MainActivity.this, activityClass));
} catch(Exception ex) {
Toast.makeText(mContext, "invalid activity name: " + activityName, Toast.LENGTH_SHORT).show();
}
}

On HTML/JS,

<script type="text/javascript">
function toTwitch() {
Android.openActivity('Twitch');
}
function toAbout() {
Android.openActivity('About');
}
</script>

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>

Dynamic intent parameter for start activity

define a util class, like following:

public class AcitivtyUtil {
public static void showActivity(Activity from, Class<?> to, boolean finish) {
Intent intent = new Intent(from, to);
from.startActivity(intent);
if (finish) {
from.finish();
}
}
}

then you can call ActivityUtil.showActivity(this, TargetActivity.class, true|false) at any Activity.

some error in your code:

public void ShowActivity(Activity act)  // here you can't pass a Activity object as parameter, you should pass Class<?>, so it should be act.class
{
Intent intent = new Intent(this, act.class);
startActivity(intent);
}

If you don't want use util class, you can also define the showActivity() in your Activity class as member method, recommend define it in BaseActivity, so you need not duplicate it in all Activity.

public void showActivity(Class<?> to, boolean finish) {
Intent intent = new Intent(this, to);
startActivity(intent);
if (finish) {
finish();
}
}


Related Topics



Leave a reply



Submit