调用finish()并重新启动应用程序后的木材重复日志

Per*_*ror 2 android timber

我的TextView上有一个onTouchListener。触摸时,我与登录,Timber.i()然后致电finish()。如果在finish()之后,我再次启动我的应用程序,然后再次单击TextView,它将记录两次,然后记录3次,依此类推...

(如果我将Timber.i()替换为普通的Log.i(),则没有问题)

// first time
Clicked

// second time
Clicked
Clicked

// etc...
Clicked
Clicked
Clicked
Run Code Online (Sandbox Code Playgroud)

木材版本:

compile 'com.jakewharton.timber:timber:4.5.1'
Run Code Online (Sandbox Code Playgroud)

工作代码:

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;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

版面:

<?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>
Run Code Online (Sandbox Code Playgroud)

小智 6

问题在于您正在“活动” onCreate方法中“植树” 。相反,请使用自定义的Applications子类并在其中种植树。


class MyApp : Application() {

    override fun onCreate() {
        super.onCreate()

        if (BuildConfig.DEBUG) {
             Timber.plant(DebugTree())
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并相应地更新您的AndroidManifest:

<application android:name="com.foo.MyApp" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"/>