Android - New Intent starts particular method

前端 未结 4 1332
心在旅途
心在旅途 2021-01-01 15:09

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 b

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 16:03

    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();
       }
    }
    

提交回复
热议问题