0

In my case, I have two classes MenuActivity, MinhaEscolaActivity. There is a method in MenuActivity called quemSouEu, it is a non static method. It also needs some properties defined on MinhaEscolaActivity's constructor method. If I instanciate a new object of MinhaEscolaActivity on MenuActivity, these properties will be null, and i'll get NullPointerException.

Is there a way to use the method quemSouEu from MenuActivity class?

Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
Rafael
  • 3,042
  • 3
  • 20
  • 36

2 Answers2

1

You can use intents to pass values between activities. You should never create an instance of activity class. Take a look at Raghav Sood's answer in the below post

Can i Create the object of a activity in other class?

Your storage options

http://developer.android.com/guide/topics/data/data-storage.html

Store the data and retrieve it when you need. Read the docs before choosing one.

You can pass data from MinhaEscolaActivity to MenuActivtiyusing intents and execute the method in MenuActivity it self

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

You shouldnt create an activity object by yourself. An activity is a main android component that is meant to be created by the system.

If you have some functions to be shared between activities, you should create another class, and instantiate an object in your activity.

If you also have data to share, you can think about some of the standar ways of sharing data, as you can read in this answer

What are the objects that you are creating on the second class constructor? you could start the activity and get the result, but youshould only do this if actually you need to show a new view or interact with the user in a diferent way, you shouldnt tell the system to run a new activity just for calling a function.

Community
  • 1
  • 1
Carlos Robles
  • 10,828
  • 3
  • 41
  • 60