0

I have 2 Activities - A and B. I have a non-static method something() in activity B. I need to call something() in activity A and something() cannot be declared as static. What is the best way to do it?

P.S. -something() doesn't start a new activity.It just performs a random action.

gambit
  • 107
  • 10
  • 1
    Don't even try this. Activities might just be classes but they are not POJOs and you should not try to access one activity from another. That's what `Intent`s are for. Instead, put these methods into a common class and call them from each activity. – Simon Aug 14 '14 at 11:32

5 Answers5

1

Maybe inheritance can help. Create a (abstract) class (lets call it C) that extends Activity and implements the something Method (either public or protected).

Afterwards create classes A and B, that extend C instead of Actvity. This way you can call something from both activities.

Something like:

public class C extends Activity {
   protected void something() {
       // ...
   }
}



public class A extends C {
    // use something() somewhere
}

public class B extends C {
    // use something() somewhere
}
Hans Hohenfeld
  • 1,729
  • 11
  • 14
1

An alternate simpler and cleaner version may be used at the expense of potentially lower concurrency in a multithreaded environment:

 public class SingletonDemo {
        private static SingletonDemo instance = null;
        private SingletonDemo() { }
        public static synchronized SingletonDemo getInstance() {
            if (instance == null) {
                instance = new SingletonDemo();
            }
            return instance;
        }

       public void yourmethod() {
       }
    }

You can use such a class as data store and include common methods.

Access it:

   SingletonDemo.getInstance().yourmethod();
Deniz
  • 12,332
  • 10
  • 44
  • 62
0

You can do something like this,

If it's shared functionality between more than one Activity, then create a base class for your activities that derives from Activity.

public class BaseActivity extends Activity
{
      // here write your common method
}


 public class B extends BaseActivity
{
     // here you can call the method defined in BaseActivity
}
Aniruddha
  • 4,477
  • 2
  • 21
  • 39
0

Depends on what something() does. You can call a method in Activity A after Activity B has ended, using http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int)

You could also just rip out the logic into another class and instantiate that class in both activities. You can also make a singleton class and have it be injected into your class using Dagger dependency injection system (http://square.github.io/dagger/). The choice depends on what you want to do.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

Below show is sample code:

Activity 1: public class MainActivity extends ActionBarActivity { ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Act2.something();
}

}

Activity 2:

public class Act2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activityq);

    something();
}

public static void something() {
    // TODO Auto-generated method stub

}

}

Shanmugapriyan
  • 953
  • 1
  • 10
  • 28