调用完成()并重新启动应用程序后木材重复日志
我在我的TextView上有一个onTouchListener。在联系方式中,我使用Timber.i()
登录,然后拨打finish()
。如果完成后(),我再次启动我的应用程序,并再次点击TextView,它会记录两次,然后3次等...调用完成()并重新启动应用程序后木材重复日志
(如果我用正常的日志替换Timber.i()。我(),没有问题)
// first time
Clicked
// second time
Clicked
Clicked
// etc...
Clicked
Clicked
Clicked
木材版本:
compile 'com.jakewharton.timber:timber:4.5.1'
工作代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Timber.plant(new Timber.DebugTree());
TextView tv = (TextView) findViewById(R.id.mytextview);
tv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Timber.i("Clicked");
finish();
return false;
}
});
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.caca.test.MainActivity">
<TextView
android:id="@+id/mytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
问题是,您在活动onCreate
方法中'种植'树木。相反,使用自定义应用程序子类并在那里植树。
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(DebugTree())
}
}
}
并相应更新您AndroidManifest:
<application android:name="com.foo.MyApp" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"/>
我想添加这个观察您的应用程序类。虽然你已经调用了finish(),但应用程序并未实际终止,Timber仍然可用。正如@cwbbowron所提到的,在你的onCreate中调用Timber.plant()只是增加了另一个记录器。 您也可以通过使用Timber.treeCount()来检查有多少个记录器正在运行来确认这一点,以了解有多少个记录器正在运行。这是一个静态的方法,并发现它非常方便.. – giulio
张贴在那里你setDbug树等 –