gambit
gambit

Reputation: 107

non-static method different activities android

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.

Upvotes: 0

Views: 2328

Answers (5)

Shanmugapriyan
Shanmugapriyan

Reputation: 985

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

}

}

Upvotes: 0

Deniz
Deniz

Reputation: 12530

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();

Upvotes: 1

EpicPandaForce
EpicPandaForce

Reputation: 81539

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.

Upvotes: 0

Aniruddha
Aniruddha

Reputation: 4487

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
}

Upvotes: 0

Hans Hohenfeld
Hans Hohenfeld

Reputation: 1739

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
}

Upvotes: 1

Related Questions