48

Suppose I have a class first.java (activity class) and I start another activity in this class (second.java - activity class).

How can I access the instance of first.java from second.java?

Can someone give me a good explanation on this... An example would be great...

0

5 Answers 5

44

If you need your second activity to return some data to your first activity I recommend you use startActivityForResult() to start your second activity. Then in onResult() in your first activity you can do the work needed.

In First.java where you start Second.java:

Intent intent = new Intent(this, Second.class);
int requestCode = 1; // Or some number you choose
startActivityForResult(intent, requestCode);

The result method:

protected void onActivityResult (int requestCode, int resultCode, Intent data) {
  // Collect data from the intent and use it
  String value = data.getString("someValue");
}

In Second.java:

Intent intent = new Intent();
intent.putExtra("someValue", "data");
setResult(RESULT_OK, intent);
finish();

If you do not wish to wait for the Second activity to end before you do some work in the First activity, you could instead send a broadcast which the First activity reacts to.

7
  • 4
    ya you are right cant0na, but i think u dint get the question correctly. he does not want the second activity 2 return some result but he actually wants 2 transfer data(context) from first activity 2 second one....
    – N-JOY
    Commented Feb 9, 2011 at 13:04
  • He also asked for an explanation and an example. A gave an example of how it could be done. Commented Feb 9, 2011 at 13:30
  • 1
    I am aware of that, but it is an example of how it can be done. I think one should be allowed to make suggestions on how to deal with the Android framework. Commented Feb 9, 2011 at 13:39
  • how using your solution i can access the first activity object in the second class. This was the question Commented Feb 9, 2011 at 13:41
  • You can comment it on the question rather then posting it as answer. Commented Feb 9, 2011 at 13:42
10

You can simply call getParent() from the child activity.

I have no clue why other answers are so complicated.

2
  • 22
    Because it's an incomplete answer. getParent() will only work on activities embedded as child, it will return null on others.
    – acib708
    Commented Sep 8, 2016 at 19:35
  • 4
    @acib708 if you start SecondActivity with startActivity, then there is no "parent-child" relationship between them. You can use startActivityForResult for callback mechanism. Or share some singleton that can be observed. Stuff like that. Commented Apr 16, 2018 at 15:02
3

Only this should work

class first
{
    public static first instance;
    oncreate()
    {
        instance = this;
    }
}

first.instance is the required thing that is accessible from the second class

2
  • There is no guarantee that your instance will still be valid when you access it from the second activity.
    – Matthew
    Commented Mar 29, 2011 at 4:46
  • 2
    You should add onDestroy() setting instance to null
    – Borzh
    Commented Oct 29, 2018 at 0:57
-3

try this if this work 4 u.........
something like this.....

class first
{
public static first instance;
oncreate()
{
instance=this;
}

public static getInstance()
{
return instance;
}

}

now from second class call first.getInstance();

you can also directly acess instance in static way like this first.instance.......
Thanks...

12
  • 4
    Remember that a static reference is alive for as long as the class exists in the jvm. So if you set the Activity to a static variable, you could run into memory issues even after the Activity has run its onDestroy method. Commented Feb 9, 2011 at 12:49
  • 1
    We need to understand why the OP wants access to the "parent" Activity. I really don't think holding a static reference and a getInstance() method is the right approach, as you are subverting the Activity lifecycle.
    – dave.c
    Commented Feb 9, 2011 at 13:01
  • 1
    What is preferable is not the question. guys read the question . reference of first class is asked from second class. Commented Feb 9, 2011 at 13:06
  • oh buddy this was just one case.... there are 2 methods of doing this... 1)try this what i propsed, 2)Simply make a object of class first in class second and call the getter of instance..(Dont make getInstance method static) do something like this in second class........ firstObject.getInstance(); make corresponding getter method in first class....
    – N-JOY
    Commented Feb 9, 2011 at 13:09
  • Guys whats the need of getInstance method if it can be accessed first.intance directly Commented Feb 9, 2011 at 13:11
-3

You can't create an activity directly. In the first activity take a static activity variable like this,

public static Activity activity;

In the onCreate do this.

activity = this;

Then in the second activity do this,

Activity activity = (your activity name).activity;

Edit: For passing data from one activity to other activity this is not the way. Above answer was to get activity instance from other activity which was initially asked.

To pass data from one activity to other activty generally use bundle. But if the data is not primitive data type, then use object class which should implement parcelable or serializable interface. Then through bundle only parcelable list of objects we can pass.

6
  • 17
    Please don't ever do this just like this. It will leak.
    – Alex
    Commented Apr 14, 2013 at 17:53
  • @Alex why it should leak?
    – Umberto
    Commented Oct 7, 2013 at 9:29
  • 6
    The easiest way to see it leaking is by rotating your device. This way the activity will be restarted (not the application) and your static variable will still hold a reference to the old activity's context until the application is killed by the system (which is not something you can control or should worry about on Android). What Debarati wrote here is considered very bad practice and everyone who can should vote it down.
    – Alex
    Commented Oct 8, 2013 at 0:09
  • To better understand, see this article : android-developers.blogspot.fr/2009/01/… (an Activity IS a Context).
    – Alex
    Commented Oct 8, 2013 at 0:27
  • Alex, the OnCreate will be called when the screen is rotated right? So the static variable will take the new activity on rotation?
    – Sathesh
    Commented Nov 6, 2013 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.