调用finish()并重新启动应用程序后的木材重复日志 [英] Timber duplicate logs after calling finish() and restarting app
问题描述
我的TextView上有一个 onTouchListener .触摸时,我用Timber.i()
登录,然后呼叫finish()
.如果在finish()之后,我再次启动我的应用程序,然后再次单击TextView,它将记录两次,然后记录3次,等等...
I have an onTouchListener on my TextView. On touch, I log with Timber.i()
and then I call finish()
. If after finish(), I launch my app again, and click again on the TextView, It will log twice, then 3 times, etc...
(如果我将Timber.i()替换为普通的Log.i(),就没有问题)
(If I replace Timber.i() by the normal Log.i(), there is no problem)
// 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>
推荐答案
问题是您正在活动"(c2)方法中植树".相反,请使用自定义的Applications子类并在其中种植树.
The problem is that you are 'planting' trees in the Activities onCreate
method. Instead, use a custom Applications subclass and plant the trees there.
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(DebugTree())
}
}
}
并相应地更新您的AndroidManifest:
And update your AndroidManifest accordingly:
<application
android:name="com.foo.MyApp"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"/>
<application
android:name="com.foo.MyApp"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"/>
这篇关于调用finish()并重新启动应用程序后的木材重复日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!