0

Good day,

I have my main activity with an object,

public Network netConnection = null;

in my main activity i then call:

    netConnection = new Network(new Network.OnMessageReceived() {
            @Override
            // here the messageReceived method is implemented
            public void messageReceived(String message) {
                // this method calls the onProgressUpdate
                publishProgress(message);
            }
        });
        netConnection.run();

Now i create a new activity and i run it with this code:

case R.id.menu_packet: {
        Intent intent = new Intent(this, PacketActivity.class);
        String id = "" + hashCode();
        intent.putExtra("id", id);
        startActivity(intent);
        return true;
    }

I have tried doing things with putExtra() in the intent etc. But i have not come right.

Is there not an easy way to just pass a reference to PacketActivity of my netConnection ?

I don't want to copy it or any thing. just be able to access the netConnection object from the PacketActivity?

Thanks

Zapnologica
  • 22,170
  • 44
  • 158
  • 253
  • Do you need to create a new activity for the PacketActivity? Why don't you create the netConnection from PacketActivity class? – Davos555 Mar 14 '13 at 11:13
  • Communication happens over that netwrokConnection from both activities. MainActivity receives data and the other sends data. – Zapnologica Mar 14 '13 at 11:26

2 Answers2

1

You can extend Application, create setter and getter method in your extended application, and then call it from new activity.

here a tutorial
useful links:
Extending Application
Extending Application to share variables globally
Android extends Application basics

e.g.

public class myApplication extends Application{
    private myType myObj;

    public void set_myObj(myType theThing){
         myObj = theThing;
    }
    public myType get_myObj(){
         return myObj;
    }    
}

then from you main activity:

((myApplication)getApplication()).set_myObj(myObj);

and from second activity:

myType myObj = ((myApplication)getApplication()).get_myObj();

and be careful with memory leaks..!

Community
  • 1
  • 1
lelloman
  • 13,883
  • 5
  • 63
  • 85
  • Ok that sounds good, but then how would i access it from the other class? Both my activities do currently `extends Activity` Can i change them to `extends Application` ? – Zapnologica Mar 14 '13 at 11:31
  • Application has to be extended just once, for you entire application, then you access it from activities in the way you can see in edited answer – lelloman Mar 14 '13 at 11:44
  • oh so the MyApplication class is separate from my two activities classes? It is like a third party? – Zapnologica Mar 14 '13 at 11:51
  • yes, you have to extend it, the same way you extend activities, see the android reference for more details about application class, it's the first link in my answer – lelloman Mar 14 '13 at 11:54
  • @user1331971 please see my edit, I also added a tutorial link – lelloman Mar 14 '13 at 15:16
  • Ok i have done as stated above, but i am getting an error on: the `netwrok = ((GlobalVars)getApplication()).getNetwrok();' second activity: error code: `Java.lang.ClassCastException: android.app.Application cannot be case to com.example.tcptester.GlobalVars` what could be the issue? – Zapnologica Mar 14 '13 at 16:05
  • did you extended application with `class GlobalVars extends Application {...` ? – lelloman Mar 14 '13 at 16:33
  • Here is my class: ` public class GlobalVars extends Application { Network network; public Network getNetwrok() { System.out.println("Get Netwrok"); return network; } public void setNetwrok(Network _network) { System.out.println("Set Network"); this.network = _network; } }` (Sorry the comment messes up the layout. ALso wat is this i am reading about on all the forums about `android:name` in the manifest file ? is that got anything to do with it? – Zapnologica Mar 14 '13 at 16:46
  • inside the application tag is the name of the class, did you set it to yourpackagename.GlobalVars? e.g. com.example.myappname.GlobalVars – lelloman Mar 14 '13 at 16:51
0

This sounds like a use case for a service:

http://developer.android.com/reference/android/app/Service.html

The thing is once the activity from which you created the Network object is destroyed e.g. to launch the new Activity then the Network object will also be garbage collected so I don't think passing a reference would help...

GreenGuerilla
  • 369
  • 4
  • 6
  • if you pass a reference of the object, GC would't destroy it, afaik. – lelloman Mar 14 '13 at 11:20
  • And also it is stored on the main activity, and if i am not mistaken this wont get destroyed unless you close the entire app? – Zapnologica Mar 14 '13 at 11:29
  • yes, by default it should stay in the activities stack, so the root activity will keep the reference until you close the app – lelloman Mar 14 '13 at 11:36