How to call a method another Activity in my Activity?

Asked

Viewed 2,075 times

0

I have a activity A and a activity B. Activity A has the method metodoUm and I needed to use this method on Activity B. Does anyone know how I can do this? I would be very grateful.

I’m getting beaten up to look for something on the Internet and I can’t find anything.

  • I don’t know if I understand you, but have you tried the Active Name? Do not forget to leave the method that Oce wants to use public, so it can be accessed from other classes... I hope to have helped :)

  • 1

    If you have this need then your system is poorly designed. An Activity cannot depend on another. One of the reasons(there is more) is that the "other" may at any time have to be discarded by the Android OS.

  • 1

    The friend @ramaral is right. But you can give a search on the method startActivityForResult(), may be an alternative. See an example of use in Soen

1 answer

1


First of all, I want to (re)remember that an activity has a limited life cycle.

When I ask the question, static the methods to share. Then just call the method as follows: activityB.metodoUm.

  • Don’t get me wrong but this is not a good solution.

  • I don’t. But can you explain why it’s not a good solution? I’m here to learn too.

  • 1

    Because context may not exist depending on the way he created his class and probably won’t be able to recover, it is necessary to change the class and create mechanisms that make it possible to call the context statically.

  • It worked exactly as I needed it here Jackowski. Thank you ein! So Diego, in my method I pass the context via parameter when I call him, so I managed to make it work right. Thanks also.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.