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...

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Arun Abraham
  • 4,011
  • 14
  • 54
  • 75

5 Answers5

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.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Eric Nordvik
  • 14,656
  • 8
  • 42
  • 50
  • 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 Feb 09 '11 at 13:04
  • He also asked for an explanation and an example. A gave an example of how it could be done. – Eric Nordvik Feb 09 '11 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. – Eric Nordvik Feb 09 '11 at 13:39
  • how using your solution i can access the first activity object in the second class. This was the question – Rohit Sharma Feb 09 '11 at 13:41
  • You can comment it on the question rather then posting it as answer. – Rohit Sharma Feb 09 '11 at 13:42
  • I'll consider doing that next time. Anyway, my answer is not accepted, so he is free to accept your answer if it is what he needs. – Eric Nordvik Feb 09 '11 at 14:16
  • ActivityGroup is deprecated by now! – bofredo Sep 02 '13 at 12:01
10

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

I have no clue why other answers are so complicated.

Petr Peller
  • 8,581
  • 10
  • 49
  • 66
  • 22
    Because it's an incomplete answer. getParent() will only work on activities embedded as child, it will return null on others. – acib708 Sep 08 '16 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. – EpicPandaForce Apr 16 '18 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

Stunner
  • 12,025
  • 12
  • 86
  • 145
Rohit Sharma
  • 13,787
  • 8
  • 57
  • 72
-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...

N-JOY
  • 10,344
  • 7
  • 51
  • 69
  • 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. – Eric Nordvik Feb 09 '11 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 Feb 09 '11 at 13:01
  • 1
    What is preferable is not the question. guys read the question . reference of first class is asked from second class. – Rohit Sharma Feb 09 '11 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 Feb 09 '11 at 13:09
  • Guys whats the need of getInstance method if it can be accessed first.intance directly – Rohit Sharma Feb 09 '11 at 13:11
  • ya buddy u r right !!!!have u read above coment i was suggesting that if you dont want 2 make static instance then simply call getInstance(), using first class object in second.... – N-JOY Feb 09 '11 at 13:14
  • how first class object you will make available in the second class. Coz you can not pass object through intent – Rohit Sharma Feb 09 '11 at 13:17
  • instance need to be static and no need of any method to access it. – Rohit Sharma Feb 09 '11 at 13:19
  • edited the answer added comment in the end... but i think most of people here dint get the question thats why cant0na got 2 upvotes..... – N-JOY Feb 09 '11 at 13:19
  • you remove the getInstance method from your code i should delete my answer. – Rohit Sharma Feb 09 '11 at 13:25
  • why 2 delete it buddy i have done it in that way....both the possibilities are there... – N-JOY Feb 09 '11 at 13:33
  • why to increase the coding effort. More over variable are made static so that they are made class variables rather then instance variable. And class variables are to be accessed directly from Class Name. methods made static are to do some calculation over static data without calling the method on instance.. – Rohit Sharma Feb 09 '11 at 13:38
-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.

Debarati
  • 3,296
  • 2
  • 18
  • 30
  • 17
    Please don't ever do this just like this. It will leak. – Alex Apr 14 '13 at 17:53
  • @Alex why it should leak? – Umberto Oct 07 '13 at 09: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 Oct 08 '13 at 00:09
  • To better understand, see this article : http://android-developers.blogspot.fr/2009/01/avoiding-memory-leaks.html (an Activity IS a Context). – Alex Oct 08 '13 at 00: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 Nov 06 '13 at 23:47
  • This answer was not to pass data between to activies. For passing data between two activities apart from primitive data type we can use serializable or parcelable etc. This answer was for some other requirement. Please check question then comment or downvote. – Debarati Sep 11 '14 at 13:46