开发者

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

开发者 https://www.devze.com 2023-03-12 19:30 出处:网络
This is a follow up to my previous question: How to Start an Intent from a contained class of an Activity

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 s开发者_如何转开发tartActivityForResult (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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号