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.
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);
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.
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 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.
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.
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.
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.
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");
...
}
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With