How to pause / sleep thread or process in Android?

后端 未结 12 889
故里飘歌
故里飘歌 2020-11-22 05:49

I want to make a pause between two lines of code, Let me explain a bit:

-> the user clicks a button (a card in fact) and I show it by changing the background of thi

12条回答
  •  悲&欢浪女
    2020-11-22 06:28

    One solution to this problem is to use the Handler.postDelayed() method. Some Google training materials suggest the same solution.

    @Override
    public void onClick(View v) {
        my_button.setBackgroundResource(R.drawable.icon);
    
        Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() {
             @Override 
             public void run() { 
                  my_button.setBackgroundResource(R.drawable.defaultcard); 
             } 
        }, 2000); 
    }
    

    However, some have pointed out that the solution above causes a memory leak because it uses a non-static inner and anonymous class which implicitly holds a reference to its outer class, the activity. This is a problem when the activity context is garbage collected.

    A more complex solution that avoids the memory leak subclasses the Handler and Runnable with static inner classes inside the activity since static inner classes do not hold an implicit reference to their outer class:

    private static class MyHandler extends Handler {}
    private final MyHandler mHandler = new MyHandler();
    
    public static class MyRunnable implements Runnable {
        private final WeakReference mActivity;
    
        public MyRunnable(Activity activity) {
            mActivity = new WeakReference<>(activity);
        }
    
        @Override
        public void run() {
            Activity activity = mActivity.get();
            if (activity != null) {
                Button btn = (Button) activity.findViewById(R.id.button);
                btn.setBackgroundResource(R.drawable.defaultcard);
            }
        }
    }
    
    private MyRunnable mRunnable = new MyRunnable(this);
    
    public void onClick(View view) {
        my_button.setBackgroundResource(R.drawable.icon);
    
        // Execute the Runnable in 2 seconds
        mHandler.postDelayed(mRunnable, 2000);
    }
    

    Note that the Runnable uses a WeakReference to the Activity, which is necessary in a static class that needs access to the UI.

提交回复
热议问题