Titanium JIRA Archive
Appcelerator Community (AC)

[AC-6216] Android: Issue after a long time of inactivity

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionDuplicate
Resolution Date2019-04-19T22:33:39.000+0000
Affected Version/sn/a
Fix Version/sn/a
Componentsn/a
LabelsTabGroup, android
ReporterRainer Schleevoigt
AssigneeShak Hossain
Created2019-04-19T09:06:13.000+0000
Updated2019-04-19T22:33:39.000+0000

Description

The new versions of Android kills apps after long time of sleeping. The requesting of BatterySafingPermission doesn't help for all devices. ;-( My observation: if I restart the app after waking up from sleeping mode then the app seems to frozen. Here the tracelog: https://gist.github.com/AppWerft/59ca215fb603f5a29394f7fb07b00227 Here my samplecode:
// root activity called by app.js:
module.exports = function() {
	const $ = Ti.UI.createTabGroup({
		fullscreen : false,
		exitOnClose : true,
		orientationModes : [ Ti.UI.PORTRAIT, Ti.UI.UPSIDE_PORTRAIT ]
	});
	$.addEventListener('open', require('ui/common/main.menu'));
	$.open();
};
And here the menu, calling after opening of tabgroup:
//  main.menu.js:
module.exports = function() {
        const lifecyclecontainer = arguments[0].source;
	console.log("_openevent");
	if (!lifecyclecontainer)
		return;
	var activity = lifecyclecontainer.getActivity();
	if (activity) {
		console.log("_openevent activity"); // last seen message!!!
		activity.onCreateOptionsMenu = function(_menu) {
			console.log("_openevent onCreateOptionsMenu");
			const menuItem = _menu.menu.add({
				icon : Ti.App.Android.R.drawable.ic_action_audio,
				showAsAction : Ti.Android.SHOW_AS_ACTION_IF_ROOM,
			});
		};
		activity.invalidateOptionsMenu();
	}
};
Summary: the activity of tabgroup is != null, but the next statement activity.onCreateOptionsMenu thows an exception. The code runs on first start of app, but crashes after resuming. This issue only appears with new SDK >=7 and >=Oreo. Timing issue?

Comments

  1. Rainer Schleevoigt 2019-04-19

    With the "Don't keep activities" on in developer options I can trigger the crash immediately: Steps: 1. Running app 2. Back button 3. Restart from Home screen => no issue 1. Running app 2. Menu button or screen off 3. Restart 4. Crash with log above
  2. Joshua Quick 2019-04-19

    [~titanium@webmasterei-hamburg.de], this is a known issue which is reported here: [TIMOB-26964] This issue is scheduled to be resolved in Titanium 8.0.1. I tested the below Classic app code with that ticket's PR and have confirmed that it'll fix your issue. It displays a menu item like how you're doing it and it's definitely recreating the menu item when the activity window is being "restored" by the Android OS (ie: after resuming the app).
       function createTab(title) {
       	var window = Ti.UI.createWindow({ title: title });
       	window.add(Ti.UI.createLabel({ text: title + " View" }));
       	var tab = Ti.UI.createTab({
       		title: title,
       		window: window,
       	});
       	return tab;
       }
       
       var tabGroup = Ti.UI.createTabGroup({
       	tabs: [createTab("Tab 1"), createTab("Tab 2"), createTab("Tab 3")],
       });
       if (Ti.App.Android) {
       	tabGroup.activity.onCreateOptionsMenu = function(e) {
       		Ti.API.info("@@@ onCreateOptionsMenu() called.");
       		var menuItem = e.menu.add({
       			title: "Menu Item 1",
       			showAsAction: Ti.Android.SHOW_AS_ACTION_IF_ROOM,
       		});
       		menuItem.addEventListener("click", function() {
       			alert("Menu item was clicked on.");
       		});
       	};
       }
       tabGroup.open();
       
    Please note that this is known to crash in Titanium 7.2.1 through 7.5.1. In Titanium 8.0.0, the TabGroup appears empty instead and doesn't crash. Still not great, but at least the crash is gone. In 8.0.1, it'll work as expected.

JSON Source