How to use CallBack function callback
This article mainly introduces how to use the callback of CallBack function, which is very detailed and has certain reference value. Friends who are interested must finish reading it.
In a project, the function callback CallBack is often used
Next, I'll give you a simple Demo because it's not clear how to pass the object.
A global variable callback is used here
/ / the main program receives the callback data
Package com.example.testtransaction;import android.os.Bundle;import android.view.View;import android.widget.Toast;import android.app.Activity;import android.content.Intent;public class MainActivity extends Activity {public static MyResultCallBack callback;// uses the global variable @ Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) Callback = new MyResultCallBack () {@ Overridepublic void getData (String data) {/ / TODO Auto-generated method stubToast.makeText (MainActivity.this, "data =" + data, Toast.LENGTH_LONG). Show ();}} public void test (View v) {Intent intent = new Intent (this, SecondActivity.class); startActivity (intent);}}
/ / callback program
Package com.example.testtransaction;import android.os.Bundle;import android.app.Activity;public class SecondActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_second);} / / press the return key to public void onStop () {super.onStop (); MainActivity.callback.getData ("callback data");}}
/ / callback API
Package com.example.testtransaction;public interface MyResultCallBack {public void getData (String data);} these are all the contents of the article "how to use CallBack function callbacks". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!