I have two classes, A1
and A2
. Both inherit from BaseActivity
, which, in turn, inherits from AppCompatActivity
.
BaseActivity
has a boatload of logging statements that track, e.g., activity lifecycle methods. Like this: Log.d(tag, "in onCreate");
Those messages are logged, as expected, from A1
but not from A2
! I've tried monitoring logcat both from AS and from command line. I can repeat the problem on several Android emulators and in a couple of dev environments.
If I simply replace, e.g., Log.d(tag, "in onCreate")
with System.out.println(tag + ": in onCreate")
, all of the expected messages appear.
Totally mystified, at this point. Anybody got any suggestions about where my log messages are going?
Edited to add:
I have replaced all calls to Log.d
with calls to the following method:
private void log(String message) {
Log.d("LOGGER", tag);
Log.d(tag, message);
System.out.println(tag + ": " + message);
}
Here is sample output from the two subclasses:
07-09 10:26:42.036 9657-9657/net.callmeike.android.latest D/LOGGER: A1
07-09 10:26:42.036 9657-9657/net.callmeike.android.latest D/A1: in onCreate
07-09 10:26:42.036 9657-9657/net.callmeike.android.latest I/System.out: A1: in onCreate
07-09 10:27:10.837 9657-9657/net.callmeike.android.latest D/LOGGER: A2
07-09 10:27:10.837 9657-9657/net.callmeike.android.latest I/System.out: A2: in onCreate
It appears that the variable tag
is, in fact, the thing that drives the issue. Anybody understand why?
FWIW:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="false"
android:theme="@style/AppTheme"
>
<activity
android:name=".A1"
android:label="@string/a1_name"
android:theme="@style/AppTheme.NoActionBar"
>
</activity>
<activity
android:name=".A2"
android:label="@string/a2_name"
android:theme="@style/AppTheme.NoActionBar"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
EDITED to add: Sources for the three Activity-related classes:
public abstract class BaseActivity extends AppCompatActivity {
private final String tag;
private final int layout;
public BaseActivity(String tag, int layout) {
this.tag = tag;
this.layout = layout;
}
protected abstract void test();
@Override
protected final void onCreate(Bundle savedInstanceState) {
Log.d(tag, "in onCreate");
super.onCreate(savedInstanceState);
setContentView(layout);
setSupportActionBar(findViewById(R.id.toolbar));
findViewById(R.id.fab).setOnClickListener(this::makeSnackbar);
}
final void makeSnackbar(View v) {
Snackbar.make(v, R.string.action_test, Snackbar.LENGTH_LONG)
.setAction(R.string.action_test, v1 -> test())
.show();
}
// blah blah blah
}
// BaseActivity logs show up in logcat
public class A1 extends BaseActivity {
private static final String TAG = "A1";
public A1() {
super(TAG, R.layout.activity_a1);
}
@Override
protected void test() {
// blah blah blah
}
}
// BaseActivity logs do not show up in logcat
public class A2 extends BaseActivity {
private static final String TAG = "A2";
public A2() {
super(TAG, R.layout.activity_a2);
}
@Override
protected void test() {
// blah blah blah
}
}