Android???????????й?
???????????? ???????[ 2016/11/27 14:05:04 ] ??????????? ???????
???????
???????????й???????????????п????????????????????й????????????????????????????????л???????????????????????й?????????????????????LeakCanary??MAT????????????ó????????????й???MAT???????????????????????????????????LeakCanary??????Square?????????????????????й?????????????????????????й?????????????????????????????????й?????????????????й??????й????????????????????顣
???????й?
???????????????й???
???????????????????????????????????????????????????????????????????????????????????????????????±??????????????????????????????У???????????й???
???????й??????????
???????й????????ó???OOM???????????????????Android????????ó??????????????????????????в????????й?????????????????????????????泬????????????????????????????????????????Crash??
????Android?г????????й?????
???????????????й?
??????????????????????????????????????????????????й??????????????????????????????????????????????????????????????????????????????????????????????????и???????????????????????????????????????????й????????????????
????public class AppManager {
????private static AppManager instance;
????private Context context;
????private AppManager(Context context) {
????this.context = context;
????}
????public static AppManager getInstance(Context context) {
????if (instance != null) {
????instance = new AppManager(context);
????}
????return instance;
????}
????}
???????????????????????????????????????????????????????Context?????????Context???????????????????????
????1?????????Application??Context????????κ???????????????????????Application???????
????2?????????Activity??Context???????Context???????Activity???????????Context??Activity?????????????????Activity???????Context??????????Activity??????????沢??????????????????????и?Activity?????á?
???????????????????????????????????
????public class AppManager {
????private static AppManager instance;
????private Context context;
????private AppManager(Context context) {
????this.context = context.getApplicationContext();
????}
????public static AppManager getInstance(Context context) {
????if (instance != null) {
????instance = new AppManager(context);
????}
????return instance;
????}
????}
?????????????????Context??????Application??Context??????????????????????????????????????????й?
????????????????????????????й?
?????е??????????????????????Activity?У????????????????????????????????????????д????
????public class MainActivity extends AppCompatActivity {
????private static TestResource mResource = null;
????@Override
????protected void onCreate(Bundle savedInstanceState) {
????super.onCreate(savedInstanceState);
????setContentView(R.layout.activity_main);
????if(mManager == null){
????mManager = new TestResource();
????}
????//...
????}
????class TestResource {
????//...
????}
????}
??????????Activity?????????????????????????????????Activity??????????????????????????????????????????????????????д???????????й?????????????????????????????????????????÷??????????????????????????????????????????????????????????t???????????и?Activity???????????Activity??????????????????????????????????????????????????????????????????????????????????????????Context???????ApplicationContext
????Handler???????й?
????Handler????????????й??????????????????????????????????????Щ????????api?????????Handler???????????Handler?????????д????淶???п?????????й????????????
????public class MainActivity extends AppCompatActivity {
????private Handler mHandler = new Handler() {
????@Override
????public void handleMessage(Message msg) {
????//...
????}
????};
????@Override
????protected void onCreate(Bundle savedInstanceState) {
????super.onCreate(savedInstanceState);
????setContentView(R.layout.activity_main);
????loadData();
????}
????private void loadData(){
????//...request
????Message message = Message.obtain();
????mHandler.sendMessage(message);
????}
????}
???????????Handler????????????й???????mHandler??Handler??????????????????????????????????Activity????????????????????????????Looper????в???????????????????????Activity????????????л???δ??????????????????????????????????е?Message????mHandler??????????mHandler?????Activity???????????????Activity??????????????????????????й?????????????????????
????public class MainActivity extends AppCompatActivity {
????private MyHandler mHandler = new MyHandler(this);
????private TextView mTextView ;
????private static class MyHandler extends Handler {
????private WeakReference<Context> reference;
????public MyHandler(Context context) {
????reference = new WeakReference<>(context);
????}
????@Override
????public void handleMessage(Message msg) {
????MainActivity activity = (MainActivity) reference.get();
????if(activity != null){
????activity.mTextView.setText("");
????}
????}
????}
????@Override
????protected void onCreate(Bundle savedInstanceState) {
????super.onCreate(savedInstanceState);
????setContentView(R.layout.activity_main);
????mTextView = (TextView)findViewById(R.id.textview);
????loadData();
????}
????private void loadData() {
????//...request
????Message message = Message.obtain();
????mHandler.sendMessage(message);
????}
????}
??????????????Handler?????????Handler???е????????????????????????????????Handler???е?????????????????Activityй???????Looper????????????л????????д?????????????????????Activity??Destroy?????Stop???????????????е???????????????????£?
????public class MainActivity extends AppCompatActivity {
????private MyHandler mHandler = new MyHandler(this);
????private TextView mTextView ;
????private static class MyHandler extends Handler {
????private WeakReference<Context> reference;
????public MyHandler(Context context) {
????reference = new WeakReference<>(context);
????}
????@Override
????public void handleMessage(Message msg) {
????MainActivity activity = (MainActivity) reference.get();
????if(activity != null){
????activity.mTextView.setText("");
????}
????}
????}
????@Override
????protected void onCreate(Bundle savedInstanceState) {
????super.onCreate(savedInstanceState);
????setContentView(R.layout.activity_main);
????mTextView = (TextView)findViewById(R.id.textview);
????loadData();
????}
????private void loadData() {
????//...request
????Message message = Message.obtain();
????mHandler.sendMessage(message);
????}
????@Override
????protected void onDestroy() {
????super.onDestroy();
????mHandler.removeCallbacksAndMessages(null);
????}
????}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11