Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use interface to communicate between two activities

I am trying to make listener interface between two activities Act1 and Act2. Act1 will start Act2. If there is some event occurred in Act2, it will inform it to Act1. Problem is that I am starting new activity using Intent, so how Act1 will assign itself as listener to Act2's interface?

Act1.java

public class Act1 extends ActionBarActivity implements         ActionBar.OnNavigationListener {      ActionBar actionbar;     Intent pizzaIntent;     boolean visibleFirstTime = true;      public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.menutab);          //set actionbar here     }  @Override     public boolean onNavigationItemSelected(int arg0, long arg1)// item pos,                                                                 // itemid     {         switch (arg0) {         case 0:             if(this.visibleFirstTime == false)             {             if(pizzaIntent == null)             {                 pizzaIntent = new Intent(this,Act2.class);                 //how to call setChangeListener?             }             startActivity(pizzaIntent);             }             else                 this.visibleFirstTime = false;             break;         case 1:             System.out.println("selected: " + arg0);             break;         case 2:             System.out.println("selected: " + arg0);             break;         case 3:             System.out.println("selected: " + arg0);             break;         default:             break;         }         return true;     } } 

Act2.java

public class Act2 extends Activity {       selectionChangeListener listener;      public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.pizza_slice_selection);     }      public void setChangeListener(selectionChangeListener listener)     {         this.listener = listener;     }      private interface selectionChangeListener     {         public void selectionMadeAtIndex(int index);     } } 

Note: Please don't suggest me to use fragments. I want to use activities currently.

like image 769
NightFury Avatar asked Sep 26 '13 11:09

NightFury


People also ask

How do you pass an interface from one activity to another?

And now you can just add the interface as an extra to the Intent which starts the other Activity : Intent intent = new Intent(context, OtherActivity. class); intent. putExtra("interface", inter); startActivity(intent);

How do you communicate between activity and activity?

This example demonstrates how do I communicate between Activity and Service in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

I would suggest to create a model class. Let me give you an example:

Model class:

public class CustomModel {      public interface OnCustomStateListener {         void stateChanged();     }      private static CustomModel mInstance;     private OnCustomStateListener mListener;     private boolean mState;      private CustomModel() {}      public static CustomModel getInstance() {         if(mInstance == null) {             mInstance = new CustomModel();         }         return mInstance;     }      public void setListener(OnCustomStateListener listener) {         mListener = listener;     }      public void changeState(boolean state) {         if(mListener != null) {             mState = state;             notifyStateChange();         }     }      public boolean getState() {         return mState;     }      private void notifyStateChange() {         mListener.stateChanged();     } } 

And here's how you would use this:

// Imports public class MainActivity extends Activity implements OnCustomStateListener {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         CustomModel.getInstance().setListener(this);          boolean modelState = CustomModel.getInstance().getState();         Log.d(TAG, "Current state: " + String.valueOf(modelState));          Intent intent = new Intent(this, SecondActivity.class);         startActivity(intent);     }      @Override     public void stateChanged() {         boolean modelState = CustomModel.getInstance().getState();         Log.d(TAG, "MainActivity says: Model state changed: " +              String.valueOf(modelState));     } } 

Changing the member state in second activity:

// Imports public class SecondActivity extends Activity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         CustomModel.getInstance().changeState(true);     } } 

LogCat output:

Current state: false

MainActivity says: Model state changed: true

like image 84
vilpe89 Avatar answered Sep 22 '22 02:09

vilpe89