Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - New Intent starts particular method

Tags:

java

android

I want to start one of my existing activities and force the activity to call a specific method after it starts. Is this possible?

Can I define a method that should be called after creating the activity inside my Intent?

For example, something like:

Intent intent = new Intent(this, com.app.max.Home.class.myMethod);
like image 342
Somk Avatar asked Oct 24 '11 13:10

Somk


People also ask

Which method creates an intent to start another activity?

Create the Second Activity. All subclasses of Activity must implement the onCreate() method. This method is where the activity receives the intent with the message, then renders the message. Also, the onCreate() method must define the activity layout with the setContentView() method.

What triggers an intent in android?

Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc. It is generally used with startActivity() method to invoke activity, broadcast receivers etc. The dictionary meaning of intent is intention or purpose.

What is the difference between intent and IntentFilter?

An intent is an object that can hold the os or other app activity and its data in uri form.It is started using startActivity(intent-obj).. \n whereas IntentFilter can fetch activity information on os or other app activities.


3 Answers

No, I don't think you can have something like this:

Intent intent = new Intent(this, com.app.max.Home.class.method);

but you can do this:

Intent intent = new Intent(this, com.app.max.Home.class);
intent.putExtra("methodName","myMethod");
startActivity(intent);

and then in the called activity (where you need to start the method), you can take the intent and decide which method to call like this:

@Override
protected void onNewIntent(Intent intent) {
   super.onNewIntent(intent);
   if(intent.getStringExtra("methodName").equals("myMethod")){
      mymethod();
   }
}
like image 55
Lukap Avatar answered Oct 16 '22 22:10

Lukap


Hello You can't call a particular method from intent but alternately you can use a service from intent and your requirement will be done.

Intent intent = new Intent(this, MyService.class);

and in MyService.class

public class MyService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
          yourParticularMethod();  //do your task and stop the service when your task is done for battery saving purpose
          context.stopService(new Intent(context, MyService.class)); 

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

and register this service in your manifest file like this

<application>
    <service android:name=".MyService" />
    .
    .
</application>
like image 41
Akash Kumar Avatar answered Oct 16 '22 22:10

Akash Kumar


I solve this issue by using onCreate instead of onNewIntent.

Activity A:

Intent intent = new Intent(this, com.app.max.Home.class);
intent.putExtra("methodName","myMethod");
startActivity(intent);

com.app.max.Home Activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    if(savedInstanceState == null)
    {
        Bundle extras = getIntent().getExtras();
        if (extras == null)
        {
            //Extra bundle is null
        }else{
            String method = extras.getString("methodName");

            if (method.equals("myMethod"))
            {
               //Call method here!
            }
        }
    }

Hope this solution solve your problem

like image 4
Melchior Avatar answered Oct 16 '22 21:10

Melchior