Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use CallBack function callback

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report