Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a method using intent

Tags:

android

Is there any possible way to call a method at the time when call the intent activity. I want to display only particular method when i switch over from one activity to another using intent call.

like image 707
RAAAAM Avatar asked May 10 '11 06:05

RAAAAM


People also ask

What is the use of Intent createChooser () method?

Most commonly, ACTION_SEND action sends URL of build-in Browser app. While sharing the data, Intent call createChooser() method which takes Intent object and specify the title of the chooser dialog. Intent. createChooser() method allows to display the chooser.

What is the putExtra () method used with Intent for?

Using putExtra() We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.

Is Intent a method?

Android App Development for Beginners An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities.

How do you call a method from another activity in Java?

The Best Answer is You can use startActivityForResult or you can pass the values from one activity to another using intents and do what is required.


1 Answers

Use the extras bundle in the Intent.

Intent i = new Intent(...);
i.putExtra("your_condition", int_condition);

Then on onCreate of the Intent

int_condition=getIntent.getIntExtra("your_condition");

Now you can use this

if(int_condition==0)
{
//Call the method
}
else
{
//method you want
}

Again there is another option for you as you can pass the method name as a parameter in your Intent, assumes that you are sending mathod_name as an extra to the Bundle

String method_name=getIntent.getIntExtra("method_name");
Class<?> c = Class.forName("class name");
Method  method = c.getDeclaredMethod (method_name, parameterTypes)
method.invoke (objectToInvokeOn, params)
like image 125
Tanmay Mandal Avatar answered Oct 14 '22 05:10

Tanmay Mandal