How to Start an Intent for Result from a contained class of an Activity (startActivityForResult)

江枫思渺然 提交于 2020-01-15 06:51:27

问题


This is a follow up to my previous question: How to Start an Intent from a contained class of an Activity

Basically, I realized that I need to call startActivityForResult (and not startActivity) from a contained class of an Activity. The answer from the previous question passed the Context from the Activity class to the contained class. However, the context alone isn't enough to call StartActivityForResult.

What is the best way to do this? Do I have to pass the entire Activity Class to the method in the contained class?


回答1:


Yes. Pass your Activity to the contained class as a parameter.

P.S. Do NOT store the reference to the Activity. Pass it in as you need it.




回答2:


Well, looking at your question, you should be able to do this:

Class TestLauncher
{
   public TestLauncher (Context c)
   {
      Intent i = new Intent(MainActivity.this, YourActivity.class)
      MainActivity.this.startActivityForResult(i, 1);
   }
}

Try that and see if it works.



来源:https://stackoverflow.com/questions/6351181/how-to-start-an-intent-for-result-from-a-contained-class-of-an-activity-startac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!