Android - New Intent starts particular method

前端 未结 4 1358
心在旅途
心在旅途 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:02

    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

提交回复
热议问题