Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...