2018-10-14  本文已影响19人  咸鱼佬

onStop()一定执行?

这个方法在activity变得完全不可见的时候调用,如果只是得到一个透明的对话框,提示框的话,onPause方法会得到执行,onStop方法不会执行。     

网上看到的

/**
 * Called when you are no longer visible to the user.  You will next
 * receive either {@link #onRestart}, {@link #onDestroy}, or nothing,
 * depending on later user activity.
 *
 * <p><em>Derived classes must call through to the super class's
 * implementation of this method.  If they do not, an exception will be
 * thrown.</em></p>   

类文件注释
那么

image

在这个demo 中 从helloworld actiity(A activity)跳转到绿色activity(B activity) ,A的onStop方法就一定会执行吗

  00:06:50.599    onActivityCreated: MainActivity
  00:06:50.643    onActivityStarted: MainActivity
  00:06:50.643    onActivityResumed: MainActivity
  00:07:27.101    onActivityPaused: MainActivity
  00:07:27.109    onActivityCreated: Main2Activity
  00:07:27.113    onActivityStarted: Main2Activity
  00:07:27.113    onActivityResumed: Main2Activity
  00:07:27.570    onActivitySaveInstanceState: MainActivity
  ---------下面是返回到MainActivity的-----
  00:07:38.771    onActivityPaused: Main2Activity
  00:07:38.779    onActivityResumed: MainActivity
  00:07:38.789    onActivityStopped: Main2Activity
  00:07:38.790    onActivityDestroyed: Main2Activity

MainActivity 并没有执行onStop方法
其实第二个Activity被设置为透明的全屏

<item name="android:windowFullscreen">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>   

而绿色只是布局文件viewGroup的背景色,假设这viewGroup不设置背景,那么就会依旧看到MainActivity的hello world 字
所以对于系统判断来说,MainActivity还是能被用户看见的,所以不会调用onStop方法

valueAnimator

打印多少次start,多少次end

ValueAnimator valueAnimator = new ValueAnimator();
    valueAnimator.setFloatValues(1, 100);

    valueAnimator.setDuration(2000);
    valueAnimator.setRepeatCount(1);

    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            MainActivity.this.findViewById(R.id.click).setTranslationX(
                    (Float) animation.getAnimatedValue());
        }
    });

    valueAnimator.start();
    valueAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
            Log.d("Color", "start");
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            Log.d("Color", "end");
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });

0次start ,1次end
addListener 在start之后才调用,onAnimationEnd函数当动画结束才会调用,并不是repeatCount决定

Kotlin属性委托

val app: Application by KotlinApp()

class KotlinApp : Application() {

    operator fun getValue(nothing: Nothing?, property: KProperty<*>): Application{
        return this
    }

}

class KotlinActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin)
        app.getString(R.string.app_name)
    }
}

一看之下并没有杀不妥的样子
val app: Application by KotlinApp()
KotlinApp() 创建了一个KotlinApp()的对象,也就是相当于这个App无缘无故多了一个Context对象

private var _app: KotlinApp? = null

val app: Application by lazy {
    _app as Application
}
class KotlinApp : Application() {

    override fun onCreate() {
        super.onCreate()
        _app = this
    }
}

或许这样感觉会好点

ArrayList

非常清楚的记得里面有个初始的容量是多少来着的

/**
 * Default initial capacity.
 */
private static final int DEFAULT_CAPACITY = 10;    

 public ArrayList(int initialCapacity) {
    super();
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
    this.elementData = new Object[initialCapacity];
}

然后我就这么玩

 val list = mutableListOf<String>()
 list.add("asdf")
 list.add(3, "sd")

卧槽,就这样看了半天,log显示是size = 1 index = 3 ,抛个异常

public void add(int index, E element) {
    if (index > size || index < 0)
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));

    ensureCapacityInternal(size + 1);  // Increments modCount!!
    System.arraycopy(elementData, index, elementData, index + 1,
                     size - index);
    elementData[index] = element;
    size++;
}

这个纯粹是个人问题,没啥好说的,一开始想到初始容量就以为在它容量之内可以任意位置插入,低级错误

上一篇下一篇

猜你喜欢

热点阅读