I just starting learning Android app development and I'm doing an app for embedded devices. My app needs to keep running for long hours but it fails to do so. I cleaned up my code of possible memory leaks but still the issue occurs.
So I tried running an app that is doing nothing, overnight, and here are my findings.
First, a snippet of the source code:
@RequiresApi(api = Build.VERSION_CODES.N)
public MainActivity()
{
super();
Log.d("TEST","NEW INSTANCE CREATED " + Calendar.getInstance().getTime()+"=================================================");
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
((TextView)findViewById(R.id.textHere)).setText(Calendar.getInstance().getTime()+"");
}
Then the overnight log:
$ adb shell am start -n "domain.removed.myapplication/domain.removed.myapplication.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 1324 on device freescale-ivg_mx6dq-1126a9d4e315e294
I/art: Late-enabling -Xcheck:jni
I/art: Debugger is no longer active
W/System: ClassLoader referenced unknown path: /data/app/domain.removed.myapplication-2/lib/arm
I/InstantRun: Instant Run Runtime started. Android package is domain.removed.myapplication, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/domain.removed.myapplication-2/lib/arm
D/TEST: NEW INSTANCE CREATED Tue Jan 25 19:00:45 GMT+08:00 2011=================================================
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
I/imx6.gralloc: open gpu gralloc module!
I/OpenGLRenderer: Initialized EGL, version 1.4
D/TEST: NEW INSTANCE CREATED Wed Jan 26 04:08:19 GMT+08:00 2011=================================================
D/TEST: NEW INSTANCE CREATED Wed Jan 26 05:10:15 GMT+08:00 2011=================================================
D/TEST: NEW INSTANCE CREATED Wed Jan 26 06:13:26 GMT+08:00 2011=================================================
D/TEST: NEW INSTANCE CREATED Wed Jan 26 07:15:13 GMT+08:00 2011=================================================
W/art: Suspending all threads took: 32.077ms
W/art: Suspending all threads took: 28.910ms
W/art: Suspending all threads took: 29.957ms
W/PathParser: Points are too far apart 4.000000596046461
Based on the logs, the app re-instantiates FIVE times.
My questions:
- Why Android is behaving this way?
- What is the best practice to handle this, and achieve my desired behavior? (app that can run for long hours without re-instantiating)
- If this is also common knowledge for Android devs, can you suggest/recommend key words, or search terms to find issues related to this?
Thanks guys in advance! :)
Joey