Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to send interface from one activity to another

I have an interface like this:

public interface MyInterface {
    public void aMethod();
}

My custom Object:

public class MyObject {

    private Context context;
    private MyInterface inter;

    public MyObject(Context context) {
        this.context = context;
        this.inter = (MyInterface) this.context;
        inter.aMethod();
    }
}

MainActivity:

public class MainActivity extends Activity implements MyInterface {

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

        MyObject my = new MyObject(this);

    }

    @Override
    public void aMethod() {
        Toast.makeText(this, "done", Toast.LENGTH_SHORT).show();
    }
}  

Here, inside MyObject constructor i can get the interface from the context and then communicate with the Activity.

But how can i send interface from one activity to another ?
I need to call a method inside Activity1 from Activity2
Is there a way like this ?

Note: i don't want to use fragment.

like image 544
palatok Avatar asked Jul 16 '14 11:07

palatok


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 can I transfer data from one activity to another in Android?

Using Intents This example demonstrate about How to send data from one activity to another in Android using intent. 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.

How do we link two activities in Android?

Intent i = new Intent(FromActivity. this, ToActivity. class); startActivity(i); In this case the Intent uses your current Activity as the Context in the first parameter, and the destination Activity in the second parameter.

How to send data from one activity to another in Android?

How to send data from one activity to another in Android using intent? This example demonstrate about How to send data from one activity to another in Android using intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

How to add a second activity in Android Studio?

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. Step 3 − Add the following code to src/MainActivity.java Step 4 − Add the following code to res/layout/activity_second.xml.

How do I send simple data to other apps?

Sending simple data to other apps. Android uses Intents and their associated extras to allow users to share information quickly and easily, using their favorite apps. Android provides two ways for users to share data between apps: The Android Sharesheet is primarily designed for sending content outside your app and/or directly to another user.

How to transfer data from one activity to another activity?

So, there can be many activities and can be a case to transfer data from one activity to the other. In such cases, Intents are used. Intents let the user jump from one activity to the other, or go from the current activity to the next activity.


2 Answers

First and foremost, this is very bad:

this.inter = (MyInterface) this.context;

If you pass a context into the constructor which does not implement your interface your application will crash and it's easy to make such a mistake. So you see, this is very error prone, instead implement it like this:

public class MyObject {

    private Context context;
    private MyInterface inter;

    public MyObject(Context context, MyInterface inter) {
        this.context = context;
        this.inter =  inter;
        inter.aMethod();
    }
}

This way it's much safer, and cleaner.


To send the Object to another Activity make sure it implements Serializable like this:

public interface MyInterface extends Serializable {
    public void aMethod();
}

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

And in the OtherActivity you can get the Object from the Intent like this:

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

    Intent intent = getIntent();
    MyInterface inter = (MyInterface) intent.getSerializableExtra("interface");

    ...
}

EDIT:

When you do something like this in your Activity you are creating an anonymous class:

OnCreateListener inter = new OnCreateListener() {

    @Override
    public void onObjCreate() {
        Log.d("pltk", "dfgbfdgh");
    }
};

The thing about such an anonymous class is that they are not static, meaning you can still access methods and variables from the class in which this listener is nested. Internally the reason for this is that this new class keeps a reference to the instance of the class which created it, in your case the enclosing Activity. This is a great thing and we use it all the time for OnClickListener etc. But in your case it is a problem because you want to send this Object to another Activity. The internal reference to the old Activity keeps it from being serialised and that is a good thing. If you could just send an Object like that you would create memory leaks like crazy because of all the old references which the garbage collector cannot collect.

The solution is pretty simple, you can either define the class in its own file or you can make the class static. Static in this context means that the class is essentially treated like it were in it's own separate file and therefore cannot access the instance of the enclosing class.

So to summarise what you have to do is either keep the class nested and define it static like this:

public class YourActivity extends Activity {

    private static class OnCreateListenerImpl implements OnCreateListener {

        @Override
        public void onObjCreate() {
            Log.d("pltk", "dfgbfdgh");
        }
    }

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

        OnCreateListener inter = new OnCreateListenerImpl();
        Intent in = new Intent(Palatok.this, SecondActivity.class);
        in.putExtra("ob", inter);
        startActivity(in);
    }
}

Or you can move the implementation in its own separate file:

public class OnCreateListenerImpl implements OnCreateListener {

    @Override
    public void onObjCreate() {
        Log.d("pltk", "dfgbfdgh");
    }
}

Although the reason why you would want to send an OnCreateListener to another Activity still eludes me, this should solve your problem.

I hope I could help and if you have any further questions feel free to ask!

like image 175
Xaver Kapeller Avatar answered Oct 17 '22 20:10

Xaver Kapeller


It is possible but such kind of Activity <-> Activity interraction will cause memory leaks. You should use LocalBroadcastManager (http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html) instead.

like image 3
Stepango Avatar answered Oct 17 '22 22:10

Stepango